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.

2

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

6 Comments

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.
you can npm install socket.io-client :) npmjs.com/package/socket.io-client
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?
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?
try installing socket.io 1.0 npm install [email protected]. maybe you have an older version (similar question stackoverflow.com/questions/22445043/…)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.