pusher.js, version 1.8

pusher_version18.png

The main change is that in Pusher Javascript versions 1.8.0 and above the pusher:subscription_succeeded event is passed an iterator object (which responds to count and each), rather than a simple array of member hashes.

Introduction

In our last status update post, we mentioned that we have been making some changes to our javascript presence API. The idea is that for large channels in particular we’d like to send less presence information immediately on subscription, and lazy load data as required. The new API gives us a whole load of extra flexibility in future in terms of what we return via the socket, and should mean that we don’t have to change the public API again.

This version also supports a new alpha feature, client events!

New presence JavaScript API

The main change is that in Pusher Javascript versions 1.8.0 and above the pusher:subscription_succeeded event is passed an iterator object (which responds to count and each), rather than a simple array of member hashes.

Before you would have wrote

presence_channel.bind('pusher:subscription_succeeded', function(members){
for (var i=0; i < members.length; i++) {
add_member(members[i]);
};
})

function add_member(member){
...
}
This changes to something like

presence_channel.bind('pusher:subscription_succeeded', function(members){
members.each(function(member){
...
})
})
We have also taken this opportunity to tweak the attributes names for member objects. member.user_id is now just member.id and member.user_info is now just member.info. This applies to the member object above, and the member passed to the member_removed and member_added functions.

We encourage all applications to upgrade to the new API as soon as possible. New features will only available in 1.8 and above, and the new javascript is also friendlier to the Pusher server :) The old API will continue to be supported for the immediate future, but it will be deprecated at some point.

Support for client events (alpha feature)

Up until now Pusher has only supported sending events via the API. This is great for many applications; it is very secure, and ties in well with applications which already post data via AJAX. However, sending events via your server adds an extra step to the process which adds latency and complexity which you don’t always need.

Pusher now allows you to write

private_channel.send('client-myevent', { your: 'data' });
The event will be sent via the socket to Pusher, then directly to all other subscribers. They can bind to it in the usual way

private_channel.bind('client-myevent', function(data) { ... });
These events are extremely low latency (on the order of 5ms on the Pusher server), and are great for sharing simple events which don’t need to be persisted or processed on the server. For example:

  • Collaborating on a drawing
  • Showing when a user starts typing in a chat application
  • Showing cursor position in a collaborative writing app

Please be careful about how many messages you trigger when using client events. For example binding to onmousemove without any buffering is a very bad idea! This is one of the reasons that this feature is currently alpha – we want to see how people use this functionality and what kind of restrictions / rate limiting make sense.

It’s also important to exercise caution when binding to client- prefixed events. For example, inserting unsanitized HTML received in a client event is probably a bad idea, depending on your trust in other subscribers.

If you’d like to take part please take a look at the docs and send us an email to be added to the alpha. We encourage you to try this out but note that as an alpha feature it may not continue to exist in it’s existing form.