Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Last active October 5, 2019 05:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikermcneil/0a4d05750768a99b4fcb to your computer and use it in GitHub Desktop.
Save mikermcneil/0a4d05750768a99b4fcb to your computer and use it in GitHub Desktop.
Examples of broadcasting to a room or emitting to a single socket in your Sails app using sails.sockets.broadcast().

How to use sails.sockets.broadcast()

Linked from: http://sailsjs.org/documentation/reference/web-sockets/sails-sockets/broadcast

Basic usage

In an action, service, or arbitrary script on the server:

sails.sockets.broadcast('artsAndEntertainment', { greeting: 'Hola!' });

On the client:

io.socket.on('message', function (data){
  console.log(data.greeting);
});

With multiple rooms

In an action, service, or arbitrary script on the server:

sails.sockets.broadcast(['artsAndEntertainment', 'currentEvents'], { greeting: 'Hola!' });

Client-side usage is exactly the same regardless which rooms are specified:

io.socket.on('message', function (data){
  console.log(data.greeting);
});

Remember that the event name is purely for identifying this message on the client; whereas room names are not visible on the client at all, and instead control which client sockets receive the message.

With a custom event name

In an action, service, or arbitrary script on the server:

sails.sockets.broadcast('artsAndEntertainment', 'foo', { greeting: 'Hola!' });

On the client:

io.socket.on('foo', function (data){
  console.log(data.greeting);
});

Omitting the requesting socket

If req is passed in as the last argument, the requesting socket will not receive the broadcasted message:

if (req.isSocket) {
  sails.sockets.broadcast('artsAndEntertainment', {
    greeting: 'Hola!'
  }, req);
}
Copy link

ghost commented Mar 1, 2017

Hey Iam using Orientdb database server. how to send message to specific id

@Crusader4Christ
Copy link

How to subscribe for artsAndEntertainment on client?

@sulthan-ti
Copy link

How can I check the success and failure of

sails.sockets.broadcast (socketRoom){
   message : data,
}

I have to do something on the success of sending a message.

@jc5826
Copy link

jc5826 commented Mar 8, 2018

The documentation makes it seem like you only need to do two things: 1) call sails.sockets.broadcast on the server 2) add io .socket.on(..) to the client. After some frustration, I found you need to do two more things:

You also need to 3) subscribe a socket to the room on the server

   subscribeToArtsAndEntertainment: function(req, res) {
        if (!req.isSocket) {
            return res.badRequest('Only a client socket can subscribe to artsAndEntertainment.  But you look like an HTTP request to me.');
        }
        var socket = sails.sockets.parseSocket(req);
        sails.sockets.join(socket, 'artsAndEntertainment');
        console.log('Subscribed socket', sails.sockets.getId(socket), 'to', 'artsAndEntertainment');
        return res.ok();
    }

.. and 4)request a subscription from the client:

  io.socket.get('/artsAndEntertainment', function(res) {
      console.log(res);
  });

I also needed to add a route to subscribeToArtsAndEntertainment in config/routes.js

@pSnehanshu
Copy link

@jc5826, how would you add the routes? And what does io.socket.get actually does. Why should it send a HTTP GET request?

@vishnumishra
Copy link

vishnumishra commented Jun 26, 2019

Thanks, @jc5826, you are the life saver :)

@sailsjs Please give the full info in the documentation.

@kevgathuku
Copy link

kevgathuku commented Oct 3, 2019

Thanks @jc5826. Really helpful info, and it helped me resolve a similar issue I was facing as well 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment