Multichannel event publishing

Multi-channel-event-publishing.png

Read how to use Pusher for multichannel event publishing.

Introduction

Pusher is a very flexible pubsub system that allows you to build all sorts of applications. However, as we start seeing the kinds of things people are building, we start hearing about optimizations that we can make to make your lives easier. One of the ones we have heard frequently is the need to publish a single event to multiple channels in one HTTP request.

Why might you need this? 

Imagine a social application where the activity of a given user should be reflected to all of their friends (like a Facebook wall). From a subscriber’s perspective, there is no single channel that they can listen on, because the stream is an aggregation of all their friends’ activity. To create the effect of an aggregated stream, we need to subscribe each user to a channel based on their login e.g. private-stream-dave. When an activity is created, we publish it to each friend of the person doing the activity e.g. private-stream-max, private-stream-phil, private-stream-martyn.

The code to do this would have previously needed to loop through the list of friends and send a message to each one of these in turn. In the past you may have included the following in a Rails application using our ruby-gem:

1def create
2  @activity = Activity.create(params[:activity])
3  current_user.friends.each do |friend|
4    Pusher.channels["private-stream-#{friend.login}"].trigger('new-activity', @activity)
5  end
6end

The new solution

We now allow you to send that same activity to all friends (channels) in one request:

1def create
2  @activity = Activity.create(params[:activity])
3  Pusher.trigger(["private-stream-max”, "private-stream-phil”, "private-stream-martyn”] , 'new-activity', @activity)
4end

You’ll notice that the first argument is an array of channels, but otherwise, the signature is the same as the old trigger method.

Behind the scenes this uses a new method on our REST API, and we will be standardising the old method to use this. Any library can be altered to use this new API, and we will be working with third party developers to make this change over the next few weeks. At the moment, the Ruby gem is the only library to have been modified – but if you’re desperate for this functionality all our libraries are open source and we’d happy accept good quality pull requests.

Update: Support for multi-channel event publishing is now present in the PHP server library and the node.js server library.

Limitations on the number of channels

While using this feature, please be aware that we have limited the number of channels that an event can be published on in a single call to 10. If you need to send to more than that, you’ll need to page through your array in chunks.

There are lots of scenarios where this can be applied, and we hope it makes the service even more useful and enjoyable to use.