socket.io/socket.io.js stuck on (pending) - Node.js - javascript

So whenever I check if the /socket.io/socket.io.js file is loaded onto my webpage is says (pending) and after a while (failed) net::ERR_CONNECTION_REFUSED
I've been looking through some other posts and trying some stuff.
I've tried using <script src="/socket.io/socket.io.js"></script>
and <script src="http://127.0.0.1:8080/socket.io/socket.io.js"></script>
My original code includes <script src="http://10.0.0.199:8080/socket.io/socket.io.js"></script> where 10.0.0.199 is my server which is running node.js and mongoDB.
My full code of my server.js file is as follows: (this file starts the socket.io)
var mongo = require('mongodb').MongoClient,
client = require('socket.io').listen(8080).sockets;
console.log('info - socket.io started');

Do not try to import Node JS script on web page
You can not play directly with socket in JS under Web Browser. Currently web browsers support only websockets.

Apparently my firewall somehow managed to block the 8080 port again.
I used iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT -m comment --comment "Socket.io port" to open the port on my firewall but this did not help. I managed to open the port using the GUI on my server.
Thanks everyone, and I hope that this might help someone in the future as this was very confusing.

Related

How to do a reverse proxy with node.js for a shiny app

EDIT
I think the problem might be that the WebSocket connection does not go through the proxy node.js. How to authorize the connection of the WebSocket?
I have a web app hosted on a nodeJS server. On the same vm I also have a shiny serveur hosting an app. I use node to redirect traffic to port 3838 (shiny) when a somes URL are requested.
I use this code on node :
app.use('/the-shiny-app', proxy({target: 'http://localhost:3838', changeOrigin: true}));
With this setting everything works fine on the shiny app when I go on mydomain/the-shiny-app/* except when I try to run code in a code box.
When I try to run code I get this error on the chrome console :
Connection closed. Info: {"type":"close","code":4503,"reason":"The application unexpectedly exited","wasClean":true}
An example of what I mean by code box :
if I do not use node.js and I redirect the traffic (on OS level) from port 80 directly to 3838 everything works fine.
sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-ports 3838
I do not know exactly what kind of exchange is made between the browser and the shiny server when we run code, but maybe the protocol used is blocked by node.js.
I found the problem. As Antony Gibbs said you need to setup a WebSocket upgrade. I'm using http-proxy-middleware you cans find the doc here : https://github.com/chimurai/http-proxy-middleware

Paho Client Web Socket Can't Find mqtt

I've been given a project that is using a Paho/MQTT client through Javascript to update a web page. The project starts an http server through python on localhost:8080 and then, when the webpage is loaded, a main.js script starts the client and runs the connect as shown below:
var client = new Paho.MQTT.Client("localhost",Number(8080),'0');
client.onConnectionLost = onConLost;
client.onMessageArrived = onMesArvd;
console.log("start connection...");
client.connect({onSuccess: onConnect});
The problem is that upon trying to connect the below error appears and the onConnect method does not appear to connect as a console.log does not appear:
WebSocket connection to 'ws://localhost:8080/mqtt' failed: Error during WebSocket handshake: Unexpected response code: 404
The code highlighted in red in the paho-mqtt.js is below:
new WebSocket(a, ["mqtt"])
I tried adding a mqtt file to the folder localhost is being run from but it only changes the response code (301 if a mqtt folder is present, 200 if a file).
Adding the mosquitto.conf that came with the project:
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
listener 1883
listener 8080
protocol websockets
I noticed it's not in the same folder as the paho/mqtt client but in ../dir2/setup/.
I've looked around but I haven't seen anyone with this issue so any help/guidance would be greatly appreciated. Thanks!
Thanks to Santosh Balaji for pointing me in the right direction on this one.
I believe I needed to install mosquitto on the pi and then change the mosquitto.conf file in the mosquitto install dir to the one provided by the project. After installing and confirming my conf was being used the js and Python connected without issue.
1) Is port 8080 occupied by other process before starting mqtt. Try to start your mosquitto with conf file. It will show up error if there is anything wrong with the start.
mosquitto -c mosquitto.conf
2) Try changing the port to 9001 as it is default port for using websockets in mqtt

How to run node.js in my web site server not my pc local server

Last 2 days I spent more time and read 50+ articles and video to understand node.js and after installation now I can see the result in browser by http//:localhost:3000/ But I have confused in many case that I describe below.
I do all of my work in my share hosting server where I my keep my web site: www.myweb.com
In every article about node.js, they are teaching how to get a result by below code in a browser by http//:localhost:3000/ in local pc server.
test.js
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://localhost:3000/');
But My Question:
If I use http//:www.myweb.com/test.js` in my browser, What will be the above code?
In case of local pc we write on npm node test.js, But In case of hosting server when any clint open the page like http//:www.myweb.com/test.js How to work it?
In case of php we used include ("head.php") to got something from that page But In this case How to make a call on node.js.
Well, what you need to do is understand how http web servers works.
Usually, on your remote machine (your server), you have an instance of a web server (ex : apache) running, which is listening to port 80 (standard port for http requests). It will handle every request made on that port, and manage routing to use the correct php/html file.
Then, it will run the php code server-side, to render an html file and serve it to the server. So the client will not see the php code at all.
Let's talk about Node.js. Node is an application that runs javascript code server-side, and can run an http server with using some modules. But the javascript code will never be shown to your client, he will only get the http response you send him (typically, the html page).
So now, with node.js, you need to do the same as the apache server did, by creating the http server. First, what you have to know is that not that many website host are offering node.js, or even console access. They usually serve the php/html files you put in the configured folder, and that's basically it. What you need is either a virtual machine, or a server on which you can install node.js and run it, or use a node.js hosting service, like heroku or nodejitsu to host your node.js http server.
So, to create the node.js http server, you need to create an http server (as you did in your code), and make it listen to port 80. Now, every http request send to your server will be handled by your node.js instance. Then, you can do anything you want with that request.
I hope I haven't been to messy.
You need to install NodeJS on the server. If this is shared hosting where you cannot install additional software then you will be unable to use NodeJS. In that case contact support of your web hosting company and inquire about NodeJS support.
On the other hand, if you do have root user or super user rights on a system, you can install NodeJS. For example for on CentOS/RHEL systems you can install using yum with the following commands.
sudo yum install epel-release
sudo yum install npm
For some of the other distributions of Linux: http://ask.xmodulo.com/install-node-js-linux.html
To access Node applications from your PC to the server, you also need to open a port in the server firewall that your Node aplication uses.

NodeJs talks to ubuntu server on EC2

Recently I have installed a nodejs app on my EC2 directory, listening to one of the port from EC2 instance. I also have a couple of executable python scripts on my EC2 ubuntu Linux server.
The way I run those executable python script is to use putty and connect to ubuntu via command line( for windows, Sure for Mac I use terminal). I am just wondering, is it possible to have the nodejs app does the same job, so I will have a UI based on the web app, which can talk to ubuntu and execute those scripts (python scripts such as "python run.py".
Please advice, thank you!
If I'm understanding correctly, the Python scripts are on the same server as the Node app, and you'd like to run them from Node.js
var execScript = require('child_process').execFile;
execScript('/path/to/python/script.py');
Should probably do the trick.
Can also add options or callbacks as needed, see the docs here: http://nodejs.org/api/child_process.html

OpenShift NodeJS deployment : socket.io index.html port assignment, etc

I locally wrote a nodeJS app using socket.io and express modules.
I wanted to use openshift for hosting.
So I changed the main .js to server.js which seems to be the index equivalent of the openshift file and changed the server port setting to:
var server = require('http').createServer(app).listen(process.env.OPENSHIFT_NODEJS_PORT || 3000);
as indicated in some posts.
However after git commit, I am still getting:
remote: info: socket.io started
remote: warn: error raised: Error: listen EACCES
remote: DEBUG: Program node server.js exited with code 0
remote:
remote: DEBUG: Starting child process with 'node server.js'
and the website doesn't work.
As the app is serving a html file, there are two more places, where the port is mentioned, which sit in the index.html that is served:
header:
<script src='//localhost:3000/socket.io/socket.io.js'></script>
and within javascript for the html file:
var socket = io.connect('//localhost:'+process.env.OPENSHIFT_NODEJS_PORT || 3000);
// intial vars and multi list from server
socket.on('clientConfig', onClientConfig);
All files and modules are seemingly uploaded, but the EACCES error still prevails.
I get the feeling that maybe the header link to localhost:3000 might be the skipping point, but I am not sure. Anyone have any idea, what the problem is?
Also, there is no : socket.io/socket.io.js file in the socket.io modules folder, which I find confusing.
I had recently developed a chat client application using socket.io and also had webrtc in it. I was able to deploy the app on openshift by making the following changes into code.
Client Side
Keep the include script tag in a relative manner like so
<script src="/socket.io/socket.io.js"></script>
While declaring io.connection, change the ip part to point the application to server in this format.
var socket = io.connect('http://yourapp-domain.rhcloud.com:8000/', {'forceNew':true });
8000 is for http and 8443 is for https
Server Side
The io and the server should both be listening on the same port and the order in which the statements are run should also be given attention.
Step 1: Declare the http server using app.
( app is obtained from express)
var express = require('express');var app = express();)
var server = require('http').Server(app);
Step 2:
Declare io from socket.io and combine it with the server object.
var io = require('socket.io').listen(server);
Step 3:
Now, allow the server to listen to openshift port and ip.
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);
Please pay special attention to the order of the statements you write, it is the order which causes issues.
The server side of your websocket needs to listen on port 8080 on your openshift ip address, the CLIENT side needs to connect to your ws://app-domain.rhcloud.com:8000
I have a few notes on how to use WebSockets here: https://www.openshift.com/blogs/10-reasons-openshift-is-the-best-place-to-host-your-nodejs-app#websockets
You don't need any additional server-side changes after adapting your code to take advantage of environment variables (when available)
OpenShift's routing layer exposes your application on several externally-accessible ports: 80, 443, 8000, 8443.
Ports 8000 and 8443 are both capable of handling websocket connection upgrades. We're hoping to add support for WebSocket connections over ports 80 and 443 soon.

Categories