0

New to node and websockets, I'm trying something to create something unlike the typical game or chat app.

I need to be able to post some variables to an action, perform some lookups and then connect to the client with the found vars. I genuinely don't know if this is possible.

The major problem I'm having is that I don't have a typical js client, no browser, nada. My only clients are python websocket clients. I have these connecting to the server OK... Well, they can ping a the server - I'm trying with Socket.io, Faye and Worlize. I'd prefer to use socket.io.

For example, if I post this:

 curl -X POST 'http://localhost:8080/timeout?id=1238763876&time=27365716576&bla=stuff' 

In my app.js I have something like this (some bits missing, my socket.io example):

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/public/index.html');
});

app.post('/timeout', function (req, res) {

  //Find socket, not implemented yet

  console.log('I received a private message by ', from, ' saying ', msg);

  io.sockets.on('connection', function (socket) {
    io.sockets.emit('this', { will: 'be received by everyone'});        
  });

});

server.listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

The route works but when I post, I get nothing bad and no errors - like I said, I'm new to Node from Rails. Am hoping it's something straight forward.

Is is possible to do such a thing? Is this the way to go about it or should I try something else.

simonmorley
  • 2,810
  • 4
  • 30
  • 61
  • It's hanging because you aren't sending any response in `app.post('/timeout')`. Sorry I don't know what you need to do to get your python websocket talking to the node websocket, I have only tried having a server websocket talking to clientside js websocket – Plato Jun 21 '13 at 17:25
  • what is your rationale for using websockets? would it make more sense to code a more usual REST api and just have the python app make requests to the node app? – Plato Jun 21 '13 at 17:26
  • @plato Yeah I know it's weird but we're not working with browsers. We have embedded devices running python. They connect to the node server already without problem but we need to issue payloads to them. Hence the need to send outbound requests. Does that make sense? – simonmorley Jun 21 '13 at 17:31
  • Oh and in your example you have `app.post('timeout'...`, is this how the python device is connecting to the node server? or did you successfully instantiate a python websocket and get it talking to your node websocket? – Plato Jun 21 '13 at 19:01
  • The example is stripped down and isn't the working inbound one. More of a proof that we can post to an action, do some shizzle and send a cmd to the clients. Right now, we have 2,000+ devices running a different python client that calls back every 45 seconds to our application and gets it's jobs. Sadly we need real tme info from them and the need to send commands instantly. Hence trying websockets. I guess I could queue a job, and 'grab' it on the next connection. Ideally i need a separate app to post yo node which fires a command off. Thanks for the help. S – simonmorley Jun 21 '13 at 19:38
  • It's like we have a pile of android devices to control but no fancy stuff and no memory. – simonmorley Jun 21 '13 at 19:45
  • Sounds like a fun project! If you can get the sockets working in python it should be straightforward to point them at the uri for the node application so the sockets can communicate. I don't think you will need to define Express routes, you can probably put everything in the socket.io callbacks - `socket.on('connection', handleConnection)` or something – Plato Jun 21 '13 at 21:16
  • Working the grey cells for sure! Should be in the pub this evening, but this is more interesting. I can connect in, I just can't can't fire something outbound restfully. That's the bit that's losing me. – simonmorley Jun 21 '13 at 21:22

1 Answers1

2

Figured this out with the following:

app.post('/commands/:action/:to', function (req, res) {
 target_socket = connections[req.params.to] 
 if (target_socket) {
   console.log(req.query)
   io.sockets.socket(target_socket.id).emit('name', 'stuff');
   res.send(200);
 }
 else
  res.send(404);
});

Inspiration taken this site..

Julian Mendez
  • 3,162
  • 2
  • 13
  • 36
simonmorley
  • 2,810
  • 4
  • 30
  • 61