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)
).
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) {
renderMessage(message);
});
The message
argument is the value you passed on the server (this
).
See the Chatroom Example for a working version of this code.
The realtime messaging features of Deployd are built on Socket.IO and work on almost every major browser:
(taken from Socket.IO's site)
Let us know if you have any ideas to improve our docs. Open an issue on github, send us an email, or tweet us.
This entire site, including documentation written in markdown is available on github. Pull requests are appreciated!