Sockets Connecting - javascript

I have been trying to get my WAMP Webserver run with socket.io but I just cannot seem to get it to work.
My socket server is on port 3000 and the WAMP on port 80. I know that both servers work.
Web Server Client Code:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://[ip address]:3000');
socket.on('buzz', function (data) {
console.log(data);
});
</script>
Console Error:
Uncaught ReferenceError: io is not defined
at (index):97
UPDATE
I was missing the socket.io.js file. But now I am getting
GET http://[[IP]]:3000/socket.io/?EIO=3&transport=polling&t=LxfQwpE
net::ERR_CONNECTION_TIMED_OUT
In the console every few seconds.

As I recall, this problem was solved by installing a NGINX reverse proxy -- I believe I followed the instructions here: Socket.io with nginx
This was one of my first projects, but I thought I would point people in the right direction in case other students wish to use a WAMP backend and Socket.io.

Related

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

ERR_CONNECTION_REFUSED http://localhost:3000/socket.io/socket.io.js

Ciao, I'm implementing a webRTC many-to-many videoconferencing system, actually, I already did it, I am using socket.IO as signalling server, and everything goes super well, I am using EnterpriseDB Apache for serving my .html file on port (8081) and Node.js for serving socket.IO on port (3000), It is working like charm in localhost, no errors, My ISSUE is serving for external access with my public IP, I am testing with my friends and these browsing services: www.browserstack.com and www.browserling.com (trial versions).
with www.browserstack.com, everything is working fine with either mozilla 42 or chrome 47
with www.browserling.com, I got these errors
Firefox 41: ReferenceError: io is not defined
Chrome 45: Failed to load resource
http://localhost:3000/socket.io/socket.io.js
net::ERR_CONNECTION_REFUSED
Uncaught ReferenceError: io is not defined
with my friends, I am having the same issues as www.browserling.com, but they are using the latest browser versions (Chrome 47 and Firefox 42) for connecting to my PC server.
I think this is not a browser version issue, The issue is on serving the socket.io.js file, finally, here is my code:
It shows just the important things in order to solve this problem:
///NODE.JS DIRECTORY
////////////////////serverside.js
var port = 3000;
var io = require('socket.io').listen(port);
io.sockets.on('connection', function (socket){.........}
///APACHE DIRECTORY
////////////////////clientside.js
//Connect to signalling server
var socket = io.connect("http://localhost:3000");
////////////////////avq.html
<!DOCTYPE html>
<html lang="es">
<head><meta charset="UTF-8">
</head>
<body>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script src="js/clientside.js"></script>
</body>
</html>
this is my server URL if anyone wants to try:
http://201.209.104.33:8081/webrtcexamples/avq.html
localhost is a special host name that points to the same computer as the one requesting it. So anyone using a different computer than yours and trying to connect to localhost will try to connect to its own computer, not yours. As the server is not running on their computer, they quite obviously get a "connection refused" error.
You need to replace localhost with a globally accessible address (domain name or IP address). This also implies that you need to have your router map your external IP address to your computer running the server for this port (otherwise they will connect to your router, not your server).
try io.connect("http://201.209.104.33:3000"); instead of io.connect("http://localhost:3000");
EDIT
You need to run the socket on the server that it is being used. So if you run it on your PC, your friends pc will not see it, because when it connects to localhost, it will try to connect to the localhost of THEIR pc. Not yours.
I had the same issue before. You need to run it off of the server so that everyone can connect to it.

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

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.

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.

'io is not defined' on client. included socket.io script tag in HTML

I am receiving these errors on the client:
GET /socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined
In HTML
<script src="/socket.io/socket.io.js"></script>
In client JS:
var socket = io.connect();
I have also tried adding the local server as some other posts suggested. (localhost:3000). I have used sockets before in this exact way without an error, so I am confused.
Try access
http://localhost:3000/socket.io/socket.io.js
directly via your web browser to see it there's a javascript after you start socket.io server. If even your web browser cannot access it, client side has nothing to do with this problem. You must check your server side if it start correctly and port 3000 hasn't been used before by another process including your previous socket.io server that may not be killed from last session. Try
ps aux | grep node
in your shell to see if there's already node process or not before start new socket.io server.

Categories