WebSync: Tutorials
On-Demand:
Server:
Fully-working copies of the examples created in these tutorials are available in the
WebSync downloads.
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
- You have completed the WebSync On-Demand: Basic tutorial.
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.
- An initial "snapshot" of the current subscribers is available in the
subscribe()onSuccesshandler. - A "difference" list of added/removed clients is sent to the
subscribe()onMetaReceivehandler 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.
-
Modify the HTML page created in the WebSync On-Demand: Basic tutorial.
- Modify the
onSuccesshandler to show the current subscribers list. - Add an
onMetaReceivehandler to thesubscribe()call.
- Modify the
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!
