Skip to content

Instantly share code, notes, and snippets.

Created September 17, 2013 18:32
Show Gist options
  • Save mikermcneil/6598661 to your computer and use it in GitHub Desktop.
Save mikermcneil/6598661 to your computer and use it in GitHub Desktop.
Using raw functionality in a Sails.js controller
module.exports = {
* Using raw functionality from a Sails.js controller
index: function (req,res) {
var socket = req.socket;
var io =;
// emit to all sockets (aka publish)
// including yourself
io.sockets.emit('messageName', {thisIs: 'theMessage'});
// broadcast to a room (aka publish)
// excluding yourself, if you're in it'roomName').emit('messageName', {thisIs: 'theMessage'});
// emit to a room (aka publish)
// including yourself'roomName').emit('messageName', {thisIs: 'theMessage'});
// Join a room (aka subscribe)
// If you're in the room already, no problem, do nothing
// If the room doesn't exist yet, it gets created
// Leave a room (aka unsubscribe)
// If you're not in the room, no problem, do nothing
// If the room doesn't exist yet, no problem, do nothing
// Get all connected sockets in the app;
// Get all conneted sockets in the room, "roomName"'roomName');
Copy link

this is awesome. Thanks

Copy link

tehmoon commented Jun 20, 2014

Took me a while to find this, but jeez it's so nice ! thanks !

Copy link

mbdwey commented Jun 21, 2014

agree with damassi 👍

Copy link

I got the same error as @steveostudios. Is something missing?

Edit : Note: req.socket is only valid if the action is triggered via a socket request, e.g. socket.get('/subscribeToFunRoom/someRoomName')
From :

Copy link

lucj commented Jul 4, 2014

Thanks, this really helps !!! Also, how would you trigger the broadcast to a room from outside of a controller ?

For instance, I'd like to broadcast a message to a particular room each time an external process create a new record in the db (I use mongo-watch in a sails service to be alerted of those record creation). Depending upon the type of record that is created, the room to broadcast to is different.

I understand that'roomName').emit('messageName', {thisIs: 'theMessage'});

can only be done from within the controller, but is there a way from a sails service (where you do not have access to the incoming request) to emit a message to the corresponding room ? (I am using sails 0.9.16).

Copy link

Thanks man, so helpful.

Copy link

heads up guys- as of 0.10.x all this stuff can now be done using the sails.sockets.* methods:

Also, in v0.11 (which will be released soon) we've upgraded to v1.0, which changes some of the underlying usage in the gist above. Fortunately, if you use the sails.sockets.* wrapper methods, you won't need to change any code (I'm taking care of the mapping under the covers)

Copy link

Where's the official docs? The above link is not working anymore, @mikermcneil.

Copy link

ghost commented Feb 19, 2015

Copy link

this is awesome if someone update the video on Youtube ~~~ , the version on Youtube was still 0.9.7~

Copy link

Documentation can now be found here

Copy link

Thanks everyone for the links! We're in the middle of some major documentation improvements (particularly in relation to sockets) which will be deployed here. In the mean time, if you're interested in contributing, you can use for previewing purposes. More info on contributing to Sails docs here.

Copy link

armgit5 commented Jul 21, 2016

do you have an example on how to connect regular nodejs client, and swift or android clients to sails js socket io server also? I'd like to see if I could connect my mobile phones to get push notification from sailsjs socket io server, thank you!

Copy link

Please help me, I want to use sails socket with android. any library for android or how to use directly with sails.js

Copy link

hi guys, i implemented but have an issue, can you look at my code and help?

Copy link

how would anyone implement this in routes ?
'GET /is/it/micky': {action: 'validate-micky', isSocket: true} < in routes.js ?

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