Connect to external server through Socket IO from Node js

0 votes

My localhost is running a node.js server (Server A); and, there is another external node.js server running at https://example.net:3000 (Server B). Server B is a dashboard site for my IoT device at home, so I cannot control or access it. But, I need it to connect using socket.io and send a specific message. I can easily use a flat javascript file (client-side) for connecting to it. However, it needs to run on the server side and be something that I can call, with, let's say an HTTP request.  And, over here(How to connect two node.js servers with websockets?), it says I could use socket.io-client from node.js with almost that exact code to achieve the said results. But, I wasn't able to connect to the socket when I ran the code from my local node.js.

Now, I know the following code works because I can see 'socket connect' in the console, and also test the socket for any message emitted towards the end of the code. So, here's the code that works successfully in flat javascript file: 

var myemail = "email@gmail.com";
var device_id = '12345';
// Create SocketIO instance, connect
var socket = io.connect('https://example.net:3000');

socket.on('connect', function(){
    try {
        console.log('socket connect');
        socket.emit('configure', {email:myemail, deviceid:device_id});
    } catch(e) {
        console.log(e);
    }
});
socket.emit("/" + device_id, "45678");

And, the following is the code that I cannot get to work when I run it from my local node.js instance. Plus, I should be getting a message saying 'socket connect' in the command line log, but I just get nothing.

var express=require('express');
var http=require('http');
var app=express();
var server = http.createServer(app);
//Variables

var myemail = "email@gmail.com";
var device_id = '12345'; 

var io = require('socket.io-client');
var socket = io.connect('https://example.net:3000');

//Connect listener
socket.on('connect', function(){
   try {
      console.log('socket connect');
      socket.emit('configure', {email:myemail, deviceid:device_id});
   } catch(e) {
     console.log(e);
   }
});
socket.emit("/" + device_id, "45678");

Anybody got any ideas?

I ran debug utility and below I'm attaching an image of its results. It seems that engine.io does an xhr poll and gets back a 503 response from the server. Now, this is definitely not a true 'temporary error' with the server as all of it is happening from running client-side js in chrome.

Nov 21, 2018 in IoT (Internet of Things) by Bharani
• 4,660 points
15,279 views

1 answer to this question.

0 votes

I think the server you're connecting to needs you to use https. So, right after the url is provided for connecting to, just add

{secure: true,    rejectUnauthorized: false}

Here's a working example:

const myemail   = email@email.com;
const device_id   = 12345;
io = require('socket.io-client');
var socket = io.connect('https://server.net:3000',{secure: true,    rejectUnauthorized: false});

function doStuff(){

//Listener
socket.on('connect', function(){
   try {
      console.log('socket connect');
      socket.emit('configure', {email:myemail, deviceid:device_id});

   } catch(e) {
     console.log(e);
   }
});

socket.emit("/" + device_id, "003021");

}

doStuff();


Hope it helps!!

To know more about Node.js, join Node.js course online today.

Thank You!!

answered Nov 21, 2018 by nirvana
• 3,130 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

How to send a data from arduino uno to a webpage through esp8266 wifi module?

You are missing a few \r\n and the length ...READ MORE

answered Aug 9, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
3,677 views
0 votes
1 answer

Running a childProcess as shell script with node.js server

Here's what I think, you could pass ...READ MORE

answered Aug 14, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
769 views
0 votes
1 answer

Transfer Data from Arduino Board to SQL Server

If you want to send it to ...READ MORE

answered Aug 29, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
4,922 views
0 votes
1 answer

How to trigger Python script on Raspberry Pi from Node-Red

Node-RED supplies an exec node as part ...READ MORE

answered Sep 14, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
3,282 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,937 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,462 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,451 views
0 votes
1 answer

Sending Web requests from my Web server to my IoT device

You need to start an HTTP server ...READ MORE

answered Jan 8, 2019 in IoT (Internet of Things) by nirvana
• 3,130 points
2,143 views
0 votes
1 answer

Connect Android Things based Raspberry Pi 3 to wifi network for the first time!

Hey, I think its alright!  Your Raspberry Pi ...READ MORE

answered Jul 19, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,183 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP