Notifying the Client of Changes with Messages #

Keeping all of the clients of your API up-to-date with your collection's latest data is simple with collection events. For example, in a PUT event, you can notify all connected clients of the changes to the object being updated by calling the emit() function to send all connected users a message.

The emit() function takes an event argument and an arbitrary data object to send to all of the connected clients (eg. emit('my message', {foo: 'bar'})). Dpd.js clients can listen for these events using the on() method (eg. dpd.todos.on('my message', fn)).

Example #

Let's say you want to make a chatroom app and you have a Collection called /messages.

In the On POST event of /messages, you would add the following line:

emit('messages:create', this);

This sends a message called messages:create (This could be anything) with the current object (this) as an argument. The messages: prefix namespaces the event to the collection.

On the client, you would listen for that event using dpd.on() and respond by updating the DOM:

dpd.messages.on('create', function(message) {

The message argument is the value you passed on the server (this).

See the Chatroom Example for a working version of this code.

Browser and Server Support #

The realtime messaging features of Deployd are built on Socket.IO and work on almost every major browser:

  • Internet Explorer 5.5+
  • Safari 3+
  • Google Chrome 4+
  • Firefox 3+
  • Opera 10.61+
  • iOS Safari
  • Android WebKit
  • WebOS WebKit

(taken from Socket.IO's site)