3

I have spent too many days on this now and been unable to get a solution.

I have a node server running perfectly (very basic) but working and have web pages that can connect and work with the server.

But what I now need to do is from an debian based Raspberry pi run a JS file using Node which can connect to my existing node server.

Is this possible or is my understanding of node incorrect.

This is my basic server

var socket = require('socket.io');
var express = require('express');
var http = require('http');

var app = express();
var server = http.createServer(app);

var io = socket.listen(server);


io.on('connection', function (client) {
    console.log('Connected');


    client.on('deviceevent', function (data) {
        io.sockets.emit('return',{ param1: data.param1, param2: data.param2, param3: data.param3 });
    console.log(data);
    });

    client.on('disconnect', function () {
    io.sockets.emit('user disconnected');
  });   
});


server.listen(3000);
console.log('Listening');

And this is how I was expecting to be able to connect via a cmd line JS file.

(This doesnt work at all)

var socket = require('socket.io');

mysocket = socket.connect('http://192.168.1.70:3000');
mysocket.emit('deviceevent', { param1: "update", param2: "0", param3: "1" });

So is it even possible? I have the server working but damned if I can get a js file that I can run at cmd line to connect.

Any help would be greatly appreceiated.

BTW. socket.io examples are all related to a web page connecting to the server which I am already doing.

Pang
  • 9,564
  • 146
  • 81
  • 122
Neil W
  • 77
  • 2
  • 6
  • You need [socket.io-client](https://github.com/socketio/socket.io-client) for second server. – laggingreflex Jan 27 '16 at 14:01
  • Possible duplicate of [How to connect two node.js servers with websockets?](http://stackoverflow.com/questions/8837236/how-to-connect-two-node-js-servers-with-websockets) – laggingreflex Jan 27 '16 at 14:03

1 Answers1

2

You need to use the socket.io-client to connect to a socket.io server

var io = require('socket.io-client');

mysocket = io.connect('http://192.168.1.70:3000');

mysocket.on('connect', function(){
  mysocket.emit('deviceevent', { param1: "update", param2: "0", param3: "1" });
});

Similar question: How to connect two node.js servers with websockets?

Community
  • 1
  • 1
Andrei CACIO
  • 2,101
  • 15
  • 28
  • Hello. Thanks for that answer and so quickly but it begs another question. Where do I get socket.io-client from. Am i supposed to install it with npm or apt-get. I have a folder in socket.io/node_modules/socket.io-client. should I be using this? Thanks. – Neil W Jan 27 '16 at 14:22
  • you can **npm install socket.io-client** :) https://www.npmjs.com/package/socket.io-client – Andrei CACIO Jan 27 '16 at 14:23
  • Ok. Thanks for that. You are correct and it is actually sending a connection to the server now because it is writing info - unhandled socket.io url to the screen. Something else wrong? Version difference maybe? or wrong thing being sent? – Neil W Jan 27 '16 at 14:44
  • Dont know if this means anything but my server has v0.10.25 node on it and says it is uptodate and my client says it has v0.10.29 and also says it is up to date. How can that be? – Neil W Jan 27 '16 at 14:48
  • try installing socket.io 1.0 **npm install socket.io@1.0**. maybe you have an older version (similar question http://stackoverflow.com/questions/22445043/node-js-socket-io-client-unhandled-socket-io-url) – Andrei CACIO Jan 27 '16 at 14:53
  • Yay. Ok, so I wasnt so far off afterall with my original idea for connection. It works. Andrei you are a genius. I am now sending from the client the connect and a parameter as displayed above and the server is showing the connection and the details I send it. Looks like the final bit was to install **npm install socket.io@1.0** to cure the url problem. Thanks – Neil W Jan 27 '16 at 15:08