Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emit an event with node.js socket client to sails.js (0.11.x) [duplicate]

The server: sails.js (0.11.x) is the server

The client: A node.js script with [email protected] and [email protected]

Big picture: I have, or will have, a farm of node.js scripts that connect to the sails.js server and will perform various tasks.

Immediate Goal: I want to emit an event during a socket connection from client->server such as:

socket.emit('got_job', job.id);

Why? If this is possible I can create various event handlers on the server side in one controller (or controller + service) and keep my code clean while managing a set of stateful transactions between client/server endpoints for supporting this script farm.

The documentation: This is how one goes about using socket.io-client for sails.js this per sails docs: https://github.com/balderdashy/sails.io.js?files=1#for-nodejs

I haven't much code to share other than what's in that link, but I'll paste it here just in case:

var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');

// Instantiate the socket client (`io`)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);

// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';
// ...

// Send a GET request to `http://localhost:1337/hello`:
io.socket.get('/hello', function serverResponded (body, JWR) {
  // body === JWR.body
  console.log('Sails responded with: ', body);
  console.log('with headers: ', JWR.headers);
  console.log('and with status code: ', JWR.statusCode);

  // When you are finished with `io.socket`, or any other sockets you connect manually,
  // you should make sure and disconnect them, e.g.:
  io.socket.disconnect();

  // (note that there is no callback argument to the `.disconnect` method)
});

What I have looked into: I've drilled into various levels of these objects and I can't seem to find anything exposed to use. And simply trying io.socket.emit() as it doesn't exist. But io.socket.get() and io.socket.post(), etc work fine.

console.log(socketIOClient);
console.log(sailsIOClient);
console.log(io);
console.log(io.socket._raw);
console.log(io.sails);

Thanks, and I'll try to update this as needed for clarification.

UPDATE:

Misc Server Info.:

  • I'm using nginx on port 443, with SSL termination, pointing to 4 (and soon more) sails.js instances on separate ports (3000-3003).
  • I'm also using Redis for sessions and sockets.
like image 648
nodnarB Avatar asked Jun 04 '15 21:06

nodnarB


1 Answers

You're close:

Your io.socket.get call is kind of like a rest api call. You'd need a sails controller bound to a get request on the url '/hello',

//client
io.socket.get('/hello', function serverResponded (body, JWR) {
  //this is the response from the server, not a socket event handler
  console.dir(body);//{message:"hello"}
});

in

config/routes.js:
{
'get /hello':'MyController.hello'
}

//controllers/MyController
{
  hello:function(req,res){
     res.json({message:"hello"});
  }
}

The method you're looking for is, io.socket.on('eventName');

Here's an example:

//client
    io.socket.get('/hello', function serverResponded (body, JWR) {
      //all we're doing now is subscribing to a room
      console.dir(body);
    });
    io.socket.on('anevent',function(message){
        console.dir(message);
    });


in 

    config/routes.js:
    {
    'get /hello':'MyController.hello'
    }

    //controllers/MyController
    {
      hello:function(req,res){
         sails.sockets.join(req.socket,'myroom');
         res.json({message:'youve subscribed to a room'});
      }
    }

What we've effectively done is, setup our socket to be part of a "room", which is basically an event namespace. Then, some other controller only has to do this:

sails.sockets.broadcast('myroom','anevent',{message:'socket event!'});

After this call is made, you would receive the message in the callback of io.socket.on('anevent').

like image 93
aclave1 Avatar answered Nov 10 '22 09:11

aclave1