PubNub FunctionsHandling Presence Events

The act of a Pub/Sub client joining, leaving, timing-out, or changing state can also act as a Function trigger.

To trigger a Function with a Presence event, use the After Presence Function type. We'll jump into an example right now.

 
Before proceeding, be sure Presence has been enabled on your keyset!
  1. Click the + symbol next to the Hello World tab to create a new Function.

    Add new EH tab
     

    The Create a New Function dialog appears.

    Create New EH Dialog
  2. For Function name, enter Presence.
  3. For Channel name, enter presence-pnpres.
  4. For Select an event, select After Presence.
  5. Click Create.

    The newly created Logger Function editor comes into focus.

    export default (request) => {
        console.log(request) // show what you got
        return request.ok()  // Return a promise when you're done
    };
  6. export default (request) => {
    
        const presence_type = request.message.action;
        const timestamp = request.message.timestamp;
        const uuid = request.message.uuid;
        const occupancy = request.message.occupancy;
        const data = request.message.data;
        const channel = request.channels;
    
        console.log("Action", presence_type);
        console.log("UNIX Timestamp", timestamp);
        console.log("UUID", uuid);
        console.log("Occupancy", occupancy);
        console.log("Data", data);
        console.log("Channel", channel);
    
        return request.ok();
    };
  7. Click Save.
  8. Click Stop Module.
  9. Click Start Module.

    Now that we've created our Function code, let's test it out!

    To test this, we'll simulate a client joining then leaving the channel via the PubNub developer console.

     
    Before proceeding, be sure Presence has been enabled on your keyset!
  10. Open a debug console tab on your browser to http://www.pubnub.com/docs/console
  11. Enter presence for the channel in the debug console.

    Switch back to the PubNub Functions GUI, and note the debug panel output. The last message printed should be Module Status: Running.

  12. Click Subscribe from the PubNub Developer Console.
  13. Switch back to the PubNub Functions GUI.

    The debug panel output should show the Join Presence Event info triggered via the PubNub Developer Console's subscribe action. The occupancy is 2 as PubNub Functions debug console is considered to be an occupant.

    Join Output
  14. Click Unsubscribe from the PubNub Developer Console.

    The debug panel output should show the Leave Presence Event info triggered via the PubNub Developer Console's unsubscribe action. The occupancy is 1 as the PubNub Functions debug console is considered to be an occupant.

    Leave Output
     

    By keying off the presence_type value, you can perform advanced logic off not only join and leave events, but also timeout and state-change events.

    When the event is specific to state-change, the state data will be found in the data attribute.