WebSync Tutorials - Frozen Mountain
Note: these tuturials are for WebSync 2 only.

If you are using WebSync 3, you want our WebSync 3 tutorials.

WebSync 2 Tutorials




On-Demand: Meta

Need to know who's arrived and left from your channel? Want to keep a list of users handy at all times? Then meta-data is what you need. In this tutorial, you will learn about the onMetaReceive handler, which gives you access to event-level notifications on channels.

Prerequisites

Getting subscription information in the client

From within the JavaScript client, there are two ways to get and maintain a list of the clients currently subscribed to a channel.
  1. An initial "snapshot" of the current subscribers is available in the subscribe() onSuccess handler.
  2. A "difference" list of added/removed clients is sent to the subscribe() onMetaReceive handler when clients subscribe/unsubscribe.
In this tutorial, we're going to illustrate how to maintain this list by printing a list of the current subscribers when we subscribe to the channel and printing a message whenever another client subscribes or unsubscribes.
  1. Modify the HTML page created in the WebSync On-Demand: Basic tutorial.
    1. Modify the onSuccess handler to show the current subscribers list.
    2. Add an onMetaReceive handler to the subscribe() call.
client.subscribe({
    ...
    onSuccess: function(args){
        for (var channel in args.data.channels) {
            util.log('Subscribed!', true);
            var ids = args.data.channels[channel].clientIds.join(', ');
            util.log('Clients ' + ids + ' are in channel ' + channel, true);
        }
    },
    onMetaReceive: function(args) {
        var event = args.data.event;
        var clientIds = args.data.clientIds;
        for (var i = 0; i < clientIds.length; i++) {
            var clientId = clientIds[i];
            if (clientId == client.getClientId()) {
                continue;
            }
            util.log('Client ' + clientId + ' ' + event + 'd!', true); // subscribed or unsubscribed
        }
    },
    ...
});

Testing

You can now open the page in a few windows to see the incoming meta notifications!