I've spent quite a while trying to connect Socket.IO-Client to a server running on Sails.js framework. The client is basically a simple JavaScript application running with Node.js on Raspberry Pi.
The idea is that a simple device connects to a server, the server then registers the device and subscribes it for messages. The device receives a message from the server and performs some actions. I don't want the client to be dependant on any framework and, for that reason, I am trying to avoid using Sailsjs-socket.io-client. At the moment both server and the client are running on my local machine. Here is the code:
// Server side. Sails.js. DevicesController.js
module.exports = {
handshake: function (req, res) {
if (!req.isSocket) return res.badRequest();
Devices.create({}).exec(function (error, data) {
Devices.subscribe(req.socket, data);
});
return res.ok();
}
};
// Client side
var socket = require('socket.io-client')('http://localhost:1337/devices/handshake');
socket.on('error', function(e) {
console.log(e);
// Here I get 'Invalid namespace'
});
So, I get the error 'Invalid namespace' on the client side and, if I'm right, it means that the server doesn't have '/devices/handshake' open. However, on the server side if I list existing room ids (sails.sockets.rooms()), I can see that a new room is always created, when the client tries to connect to the server.
I tried to connect to '/devices/handshake' from a browser with the javascript below and apparently it worked. It obviously runs with Sailsjs-socket.io-client though.
io.socket.get('/devices/handshake', function (data, jwres) {
console.log(jwres);
});
io.socket.on('devices', function (data, jwres) {
console.log(data);
});
Any ideas what am I doing wrong?