Forked from mikermcneil/using-raw-socket-io-in-sails.js
Last active
August 29, 2015 14:24
-
-
Save GQAdonis/a13663a8f9cf076e93b4 to your computer and use it in GitHub Desktop.
Revisions
-
mikermcneil created this gist
Sep 17, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ 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'); } };