You seem to be confusing a few concepts here: the Socket.io namespace, Socket.io paths, and Sails routes. You can learn a little more about the first two by looking at Socket.io docs . The main thing to keep in mind with regard to Sails is that it only listens for connections in the default / namespace; therefore, you should always connect client sockets with:
var socket = require('socket.io-client')('http://localhost:1337');
Adding /devices/handshake to the end does not change the URL that the socket is trying to connect to; it will always try to connect to http://localhost:1337/socket.io . Instead, this means that it will try to register a socket with the namespace /devices/handshake , which Sails does not provide.
On the other hand, when you call io.socket.get('/devices/handshake') , you use the Sails socket client library to make a virtual request for this route in your application, just as if you were using AJAX ( e.g. $.get('/devices/handshake') in jQuery). That is why sails.io.js was created - it makes it very simple! It also does not bind you to the interface; all sails.io.js does provide some wrappers to make it easy to interact with Sails backends through sockets. Under the covers, io.socket.get simply uses the Socket.io-client .emit() method to send a get event to the server with a payload describing the URL and parameters of the Sails action to be performed. So this is:
io.socket.get('/devices/handshake', function(body, res){...})
equivalent to plugging in your own socket and doing this:
socket.emit('get', {url: '/devices/handshake'}, function(res){...})
The recommended approach to start the logic for the socket when it is first connected is to allow the socket to fully connect to the client, and then make a request from the client to the server (exactly what you do in the second code block). To learn more about this, see this note in the Sails 0.11.x migration guide . This note also explains that if you must start the logic immediately after connecting to the server for any reason, you can do
sails.io.on('connect', function (newlyConnectedSocket){})
in your bootstrap ( config/bootstrap.js ).
sgress454
source share