Sample Code

Visitor and agent side sample code for implementing Glance Presence.

Visitor-Side Sample Code

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

// 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 for agent messages.  
presencevisitor.onsignal = function (msg) { 
  console.log("Received", msg); 
};

presencevisitor.connect();

Agent-Side Sample Code

JAVASCRIPT
// Authenticate the agent
GLANCE.Authorization.authorize({
  service: "presence",
  credentials: {
    username: "[Glance account username]",
    password: "[Glance account password]",
    gssnid: "[Glance website session id]",
    partnerid: "[Partner id, usually same as groupid]",
    partneruserid: "[Partner user id]",
    loginkey: "[Login key signed with secret api key]",
    g4scredentials: "[Glance for Salesforce credentials]"
  },
  // Not all credentials fields are required.  
  // See the page on Authorization Token for requirements.
  groupid: "1234",
  duration: "[duration of the authorization token in minutes 1-120]",
  onsuccess: showpresence,
  onfail: function(reason) {
    // display error ...
    console.error("Authorization failed:", reason);
  }
});

// Define the global presence agent variable
var presenceagent;

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

  // 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
    myhandlevisitor();

    // Optional, lookup the visitor to find any associated data
    presenceagent.lookupVisitor({
      onsuccess: function(visitordata) {
        myhandlevisitor(visitordata);
      },
      onfail: function(reason) {
        console.error("Lookup failed:", reason);
      }
    });
  };
}

// Invoke a visitor side api. Put this code behind a "cobrowse" button.
function invokeCobrowse() {
  presenceagent.invokeVisitor({
    func: "GLANCE.Cobrowse.VisitorUI.showTerms",
    args: {
      sessionKey: "111111111"
    }
  });
}

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