Skip to content
3rd-Eden edited this page Jun 29, 2011 · 12 revisions

#Message flags

Message flags are used to indicate how we should send the event or message to the user. These flags can be chained together without any particular order.

Available flags

broadcast

Sends the message or event to every connected user in the current namespace namespace, except to your self.

var io = require('socket.io').listen(80);
io.of('/namespace').on('connection', function (socket) {
  socket.broadcast.send('Hi, a new user connected');
});

json

Sends the supplied message through JSON.stringify before it sends it to the user. If you are sending objects, arrays etc without the JSON flag we will perform a toString operation on it.

var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
  socket.json.send({foo:'bar'});
});

volatile

This is basically fire and forget functionally, these messages are not buffered internally for when a client is unable to receive messages for example if the client has network issues or if the client uses a polling transport and is in the middle of a request/response cycle.

So if it doesn't matter if your client misses a couple of messages or events you might want to send these as volatile messages.

var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
  setInterval(function () {
   socket.volatile.send("it's: " + new Date);
  }, 500);
});

Clone this wiki locally