Presence Agent API

The Presence Agent API can only be used from a trusted domain.

NOTE: Contact Glance to ensure that your website domain is configured as an allowed agent origin on our Presence Service.

Authorization

The Presence Agent API requires an Authorization Token which can be obtained from the Glance Authorization Service web service. Authorization should be performed once to obtain a token which can be used for a set duration.

Please refer to the Glance Authorization API for information on obtaining and using an Authorization Token.

The Glance Presence Agent API is defined in the namespace GLANCE.Presence.Agent.

GLANCE.Presence.Agent(params) – constructor

Constructs a Presence agent object. A separate agent object must be instantiated for each visitor that the agent needs to communicate with.

Parameters

{
  "presenceserver" : [Presence Service, defaults to<a href="https://presence.glance.net/"> presence.glance.net</a>],
  "groupid: ,
  "visitorid":
}

GLANCE.Presence.Agent.prototype.lookupVisitor(params)

{
  "onsuccess" : [called back with visitor presence info if lookup succeeds],
  "onfail" : [function(errorcode) {} called back if lookup fails]
}

GLANCE.Presence.Agent.prototype.signalVisitor(params)

Sends a signal (message) to a visitor who has been connected within the last 5 seconds to the service. If the visitor has an open connection to the service, the visitor will be signaled immediately. Otherwise, the visitor will be signaled on next connect, if within 5 seconds.

The on success callback is called when the signal is confirmed that it was received by the Presence service.

Parameters

{
  "data" : [message to send to visitor],
  "onsuccess" : [function to be called back on success],
  "onfail" : [function to be called back on failure]
}

The visitor receives the signal via the onsignal event

GLANCE.Presence.Agent.prototype.invokeVisitor(params)

Invokes a visitor side JavaScript function in the GLANCE namespace. Visitor side must have been connected at some point within the last 5 seconds.

The onsuccess callback is called when the invoke message is confirmed that it was received by the Presence service.

Parameters

{
  "func" : [name of function to invoke, must be in the GLANCE namespace],
  "args" : [object to pass to the function ],
  "onsuccess" : [function to be called back on success],
  "onfail" : [function to be called back on failure]
}

GLANCE.Presence.Agent.query(params)

Queries Presence data // not implemented yet

GLANCE.Presence.Agent.prototype.connect(params)

Connect to the Presence server to receive notification of events related to the visitor. connect() opens a WebSocket connection to the Presence service.

Before calling connect(), set event handlers as properties of the Agent object.

GLANCE.Presence.Agent.prototype.disconnect()

Disconnect the Presence Agent from the presence service.

Agent Events

Events which may fire on the agent side are:

Event Data Passed
visitornotpresent
{
"connected" : [true |false ]
]}
onpresence
{
the presence data just sent by the visitor
}
onerror
{
"error" : [error code],
"detail" : [error detail]
}
Error codes:
connfail – Failed to establish or maintain the websocket connection. In the event of a connection failure, a "detail" property will include a detailed reason:
authorization – Authorization token missing, bad, or expired
connection – Network connection dropped
error – Server returned an error
onterms
{
status : ["displayed|accepted"|"declined"]
}
The terms event fires when the terms and conditions dialog is displayed, accepted, or declined by the visitor.
onvisitorsessionstart,
onvisitorsessionend,
onvisitorerror
These events fire when the corresponding events sessionstart, sessionend, and error fire on the visitor side. See the GLANCE.Cobrowse.Visitor API documentation for details.
onvisibility
{
visibility : ["hidden"|"visible"]
}
The visibility event fires when the visitor's browser tab visibility changes.

Sample Code

Visitor Side Sample Code

// Instantiate a Visitor presence object
  var presencevisitor = new GLANCE.Presence.Visitor({
  groupid : 123,
  visitorid : "111111111"
});

// Start sending regular presence updates (by default, every 60 seconds)

presencevisitor.presence();

// Send a presence update (now) with some custom data
presencevisitor.presence({ data : { wizardpage : 3} }

// Listen to agent messages.  
presencevisitor.onsignal = handleSignal;
presencevisitor.connect();

Agent Side Sample Code

// Authenticate the agent
GLANCE.Authorization.authorize({
  service: "presence",
  credentials: {
      ...
  },

  groupid: "1234",
  duration: 20,
  onsuccess: showpresence,
  onfail: function(reason) {
      // display error ...
  }
});

function showpresence() {
  // Construct a new Presence Agent object
  presenceagent = new GLANCE.Presence.Agent({
      visitorid: "12345678"
  });

  // Setup event handlers
  presenceagent.onvisitorconn = function(e) {
      // visitor is connecting via websocket and can be signaled
      // display a "connected" status, e.g. light up a cobrowse button
  };

  presenceagent.onpresence = function(e) {
      // visitor posted new presence information
      // display presence information, e.g. new e.url
  }

  // Connect the agent so it can receive the above events
  presenceagent.connect();

  // Lookup the visitor to see if he is already present
  presenceagent.lookupVisitor({
          onsuccess: function(visitordata) {
              if (visitordata.visitorpresent)
                  onvisitornavigate(visitordata);
          },
          onfail: function(reason) {};
      }
  });
}

// Invoke a visitor side api.  Put this code behind a "cobrowse" button
presenceagent.invokeVisitor({
  func: "GLANCE.Cobrowse.Visitor.startSession",
  args: {
      sessionKey: "fjpvlsf9wsd90w"
  }
});

// Send a custom signal.  onsignal will be called on the GLANCE.Presence.Visitor instance.
// For example, put this code behind a "start chat" button
presenceagent.signalVisitor({
  chatid: "123456",
  command: "startchat"
});

By continuing to use the site, you agree to the use of cookies. Learn More