Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using raw socket.io functionality in a Sails.js controller
module.exports = {
/**
*
* Using raw socket.io functionality from a Sails.js controller
*
*/
index: function (req,res) {
var socket = req.socket;
var io = sails.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
socket.broadcast.to('roomName').emit('messageName', {thisIs: 'theMessage'});
// emit to a room (aka publish)
// including yourself
io.sockets.in('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
socket.join('roomName');
// 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
socket.leave('roomName');
// Get all connected sockets in the app
sails.io.sockets.clients();
// Get all conneted sockets in the room, "roomName"
sails.io.sockets.clients('roomName');
}
};
@damassi

This comment has been minimized.

Copy link

@damassi damassi commented Nov 19, 2013

I feel like this should definitely be included in the primary documentation on Sails / Socket.io. It took me quite a while to find this.

@lloop

This comment has been minimized.

Copy link

@lloop lloop commented Dec 28, 2013

I agree with damassi

@steveostudios

This comment has been minimized.

Copy link

@steveostudios steveostudios commented Jan 29, 2014

I've been racking my brain on this and I'm certain I am missing something here.
In my controller, I am trying to join (create) a room... Should be easy, right?

index: function (req, res) {
  var socket = req.socket;
  var io = sails.io;
  socket.join('roomName');
}

and I get:

TypeError: Object #<Socket> has no method 'join'

Is there something somewhere else that I need to do?

@mikusondhi2007

This comment has been minimized.

Copy link

@mikusondhi2007 mikusondhi2007 commented Feb 3, 2014

same problem occurs with me

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Feb 27, 2014

I found the solvent that the above problem.
To use the socket.join method, there seems to be a need to send a request with WebSocket.

// api/controllers/TestController.js
module.exports = {
  join: function(req, res) {
    var room = req.param('room');

    // If request from WebSocket, this method is exist.
    req.socket.join(room);

    res.json({
      success: true
    });
  }
};
// client
socket.get('/test/join', {
  room: 'test'
}, function(response) {
  // do something
});
@tortillaj

This comment has been minimized.

Copy link

@tortillaj tortillaj commented Mar 20, 2014

Thanks for posting this mikermcneil!

@nixon1333

This comment has been minimized.

Copy link

@nixon1333 nixon1333 commented Jun 18, 2014

this is awesome. Thanks

@tehmoon

This comment has been minimized.

Copy link

@tehmoon tehmoon commented Jun 20, 2014

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

@mbdwey

This comment has been minimized.

Copy link

@mbdwey mbdwey commented Jun 21, 2014

agree with damassi 👍

@benji-bou

This comment has been minimized.

Copy link

@benji-bou benji-bou commented Jun 24, 2014

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 : https://github.com/balderdashy/sails-docs/blob/master/reference/Sockets/sails.sockets.join.md

@lucj

This comment has been minimized.

Copy link

@lucj 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

 socket.broadcast.to('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).

@jay915

This comment has been minimized.

Copy link

@jay915 jay915 commented Oct 20, 2014

Thanks man, so helpful.

@mikermcneil

This comment has been minimized.

Copy link
Owner Author

@mikermcneil mikermcneil commented Dec 18, 2014

heads up guys- as of 0.10.x all this stuff can now be done using the sails.sockets.* methods: http://sailsjs.org/#/documentation/reference/websockets/sails.sockets

Also, in v0.11 (which will be released soon) we've upgraded to socket.io 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)

@gustavohenke

This comment has been minimized.

Copy link

@gustavohenke gustavohenke commented Feb 16, 2015

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

@ghost

This comment has been minimized.

@qianjiahao

This comment has been minimized.

Copy link

@qianjiahao qianjiahao commented May 31, 2015

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

@goliatone

This comment has been minimized.

Copy link

@goliatone goliatone commented Jan 14, 2016

Documentation can now be found here

@mikermcneil

This comment has been minimized.

Copy link
Owner Author

@mikermcneil mikermcneil commented Feb 1, 2016

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 http://next.sailsjs.org/documentation/reference/web-sockets/sails-sockets for previewing purposes. More info on contributing to Sails docs here.

@armgit5

This comment has been minimized.

Copy link

@armgit5 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!

@mukulsoftwap

This comment has been minimized.

Copy link

@mukulsoftwap mukulsoftwap commented Mar 27, 2018

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

@sadeghianme

This comment has been minimized.

Copy link

@sadeghianme sadeghianme commented Jul 10, 2019

hi guys, i implemented socket.io but have an issue, can you look at my code and help?
https://stackoverflow.com/questions/56963376/angular-8-using-sails-js-websokcet-sokcet-io

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