Socket.io initialization resulting in a 404 network error - javascript

I am using nodejs and express to create a basic chat app and I'm getting a network 404 error message when trying to initialize the socket.io object.
<script src="/javascripts/socket.io/socket.io.js-client"></script>
<script type="text/javascript">
var socket = io.connect();
</script>
Tha above code results in a 404 error for some polling call
"NetworkError" 404 Not Found - http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1432851505880-89
I need the polling to run using the following url instead: http://localhost:3000/javascripts/socket.io/?EIO=3&transport=polling&t=1432851505880-89
because I am using express, but not sure how to accomplish this.
Server Side Code:
var app = express();
var server = require( "http" ).createServer( app );
var io = require("socket.io").listen(server);
server.listen(8888);
io.sockets.on('connection', function(socket){
socket.on('send message', function(data){
io.sockets.emit('new message', data);
});
});
module.exports = app;
I have been trying to troubleshoot this one for quite a while now with no success. I appreciate any advice. Commenting out the var socket = io.connect() resolves the error. Appreciate any advice.
Thanks

Your web page is apparently running on port 3000, but your socket.io server is listening on port 8888. The two must be the same port so it is no surprise that there is no response for a socket.io request on port 3000 (since your socket.io server is listening on port 8888).
Because the default URL it is trying is port 3000, then that must be the port that your web page is one and it must be served by a different web server. If that is the case, then you will need to do one of two things:
Combine the web server that serves your web pages with the socket.io server so the same server is taking care of both.
Specify the port in the client request and enable your socket.io server for cross-origin requests.
To specify the port in the client request, you can do this:
<script src="http://myserver.com:8888/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io("http://yourserver.com:8888");
</script>
Note, that this is requesting the client socket.io library from the express server where your socket.io server is (so it's using the same port that your socket.io server is running on).

If the HTML where socket.io client javascript is being served is not also being served by express at port 8888 (it looks like the html is coming from port 3000), you may just need to configure the socket.io client to point at the port where the server-side socket.io has been setup to listen (8888):
var socket = io.connect('localhost:8888');

Related

How to run socketIO server together with main server on nodejs

Currently, we have a main server that is being hosted on localhost:3000 but to run our socket.io function, we need to run it on the same server. However, we need to run it separately (npm start separately). Is there a way to run it together or on the same server without it crashing?
You cannot run socket.io in a separate process, but on the same port as some other web server in some other process. The OS will not allow that as only one process can have a listening server on a specific port. If they are in the same process, that's easy as pie as socket.io is built to share an http server in the same process (one listening server internally, traffic divided between the two uses). But, not from separate processes.
To do that, you'd have to use something like nginx on your port 3000 to proxy plain web requests to one server on some other port say 3001 and socket.io requests to another server on some other port say 3002. The client would only deal with port 3000 and nginx would direct the traffic to the right server on different ports.
I'm thinking that when you say "npm start separately", you must have some other problem you're trying to solve with that statement and we could probably help with a better way to solve that actual problem (if you disclosed what that actual requirement is) while keeping socket.io and the http server in the same process and thus no need for a proxy to divide the traffic between two separate servers.
For example, you could start up your web server with no socket.io server started and then you could tell your web server process to start up the socket.io server later. Or you could start both the web server and socket.io server in the same process at initialization time, but have a temporary server configuration that blocks incoming socket.io connections until some other requirement is met.
But, without understanding what the real requirement is, you're just lobbing us an XY problem where you describe your attempted solution rather than the actual problem that needs to be solved. When we explain that your attempted solution is the wrong way to go, we need to know what the real problem is to help further.
This is simple SocketIo Server Code.
It's not a client code.
You have to download SocketIO at npm.
const express = require('express');
const http = require('http');
const SocketIo = require('socket.io');
const app = express();
app.set('view engine', 'pug');
app.set('views', __dirname + '\\views');
app.use('/public', express.static(__dirname + '/public'));
app.get('/', (req, res) => res.render('home'));
const handleListen = () => console.log('Listening on http://localhost:3000');
const httpServer = http.createServer(app);
const wsServer = SocketIo(httpServer);
wsServer.on('connection', (socket) => {
console.log('someone joined!')
socket.on('join_room', (roomName) => {
socket.join(roomName);
socket.to(roomName).emit('welcome');
});
});
httpServer.listen(3000, handleListen);
For more Info visit official documentation.
https://socket.io/get-started/chat

Connecting 2 servers via Node Websockets

I have 2 Raspberry Pi running on the same Network.
I am using one as a local web server for my house, I then have another one connected to some devices. I want them to both be able to communicate to each other via web sockets but am having some problems.
My server looks like this:
express = require('express'); //web server
app = express();
server = require('http').createServer(app);
io = require('socket.io').listen(server); //web socket server
server.listen(8080); //start the webserver on port 8080
app.use(express.static('public')); //tell the server that ./public/ contains the static webpages
io.sockets.on('connection', function (socket) { //gets called whenever a client connects
socket.on('Testing',function(data){
console.log("Testing connection");
});
});
My problem comes with the client connection I am really not sure what code to use to try and connect.
I have installed Express and Socket.io on my client and used this code:
console.log('1');
// Connect to server
var io = require('socket.io')
var socket = io.connect('http://192.168.1.138:8080', {reconnect: true});
console.log('2');
// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected!');
});
console.log('3');
But this leads to an error on the io.connect is not a function.
I am not really sure how to get the client to work so any advice is appreciated.
I should add that connecting to my webserver directly via the ip and port does load the webpages I have created successfully.
When using socket.io on the server side as a client you need to do var socket = require('socket.io-client')('http://192.168.1.138:8080', ...);. See https://www.npmjs.com/package/socket.io-client

Given a server on Node. js, How do I listen to the calls sent to the server from WebSocket?

When I run the server, I wanted to be able to listen to the messages coming in to the server. However, the program/server is only set up to receive the calls without any notice
server.js
var express = require("express"),
program = require("program"),
app = express.createServer();
app.use(express["static"](__dirname + "/../"));
app.listen(5000);
//app.server to clients
program.init({
oscPort: xxxx,
oscHost: "xxx.xxx.xxx"
socketPort: app
});
You should add socket server because HTTP and Socket it's different protocols. There is a bunch of WebSocket servers like ws, socket.io so you could pick the one which is most suitable in your case. Here is an example how to use Socket.io with Express.

Socket.io connection local from internet

I have to make a system using socket.io there is the server in local (without port forwarded) (socket.io listen on the port 6255) and the client (socket.io.js) on a web server online hosted by hostinger.
I try to communicate between this two server but I have a connection timed out every time.
Connection on the server side : http://pastebin.com/xAFkserq
Connection on the client side : http://pastebin.com/ZMvbR3hC
Is it possible to communicate between two "networks" ? Or it's just a coding problem ?
Thanks for the help :)
Aren't you missing this?
io.on('connection', function(socket){
console.log('connected');
});
Also, have you tried following the examples in Socket.IO documentation?
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Working with sockets might be tricky, check if your network is blocking the exchange of messages somehow.
You shouldn't call connect on the client side with socket.io
On the client side simply do: var socket = io(); to connect back to the server, once the client JS has been served
Edit:
Since you're not using express and intend to just have a socket.io listener, the examples here should provide the information you need

Node.js socket.io.js not found or io not defined

I'm trying to run a node.js application on my freebsd server, but I can't get the socket.io library to work with it. I've tried including:
<script src="/socket.io/socket.io.js"></script>
Which gives a 404 error, and if i link directly to the file (i.e. where it is in my public_html folder) I get the io not defined error.
Thanks in advance
Try creating another node.js application that has this single line in it and then run it with node.js
var io = require('socket.io').listen(8000);
Then in your browser visit http://127.0.0.1:8000 and you should get the friendly "Welcome to socket.io." greeting. If you are getting this then socket.io is running and will serve the socket.io.js file.
The only other thing that I can think of that might be happening is that you might not be linking to the alternate port in your client file. Unless you're running the socket.io server on express which is running on port 80. For now create a client file that has the script source for socket.io set to
<script src="http://127.0.0.1:8000/socket.io/socket.io.js"> </script>
This should connect to the socket.io server running on port 8000 and get the socket.io.js file.
Your node.js application still has to serve it - it does not get served automagically. What do you have in your server? It should be something like
var app = require('express').createServer();
var io = require('socket.io').listen(app);
or similar (the listen is important). The location is not a real location on the disk - socket.io library should intercept the URL and serve its clientside library, as far as I understand.
Add the following after body parser:
, express.static(__dirname + "/public")
So something like:
var app = module.exports = express.createServer(
express.bodyParser()
, express.static(__dirname + "/public")
);
For those who got the same kind of issue if they run (open) your html file directly from your local file directory(ex: file:///C:/Users/index.html).
Solution: You have to run(open) the file through localhost (ex: http://localhost:3000/index.html) where the server is listening to.
Below code snippet shows how to create a server and how to wire together with the express and socket.io
const express = require("express");
const app = express();
const httpServer = require("http").createServer(app);
const io = require("socket.io")(httpServer);
///////////////////////////////////////////////////////////////
// Any other server-side code goes here //
//////////////////////////////////////////////////////////////
httpServer.listen(3000, () => {
console.log(`Server listening to port 3000`);
});

Categories