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.