-
Notifications
You must be signed in to change notification settings - Fork 0
Messaging
#Message flags
Message flags are used to indicate how Socket.IO should send the event or message to the connected client.
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');
});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'});
});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);
});The message flags are chain-able and can be used together with other flags. And this can be done in any order that makes sense to you.
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.broadcast.volatile.json.send({'foo':'bar'});
socket.volatile.broadcast.emit('ping', 'pong');
socket.json.broadcast.send([{foo:'bar'}, {'ping': 12}]);
});