Socket.io configuration resulting in error 404 - node.js

I tried to setup socket.io into my node server in order to make a messaging function for my app but having problem getting to function despite doing test app correct.
server.js:
/* long list of requires */
var express = require('express');
var http = require('http');
var https = require('https');
var config = require('./config');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('a user connected');
});
/* ssl setup */
// Create an HTTP service.
http.createServer(app).listen(8000); // opens the port
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(4433); // for the ssl
index.html
..bunch of code like bootstrap and angular.js
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.1/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8000');
socket.on('connect', function(data) {
socket.emit('join', 'Hello World from client');
});
</script>
I tried to run the site on localhost:8000.
Despite all that, I'm getting error 404 in the console:
GET /socket.io/?EIO=3&transport=polling&t=LnM0Fd3 404
I'm not sure what went wrong with the set up process of socket.io

You're creating three servers, only one of which gets the Socket.io
// createServer No 1
var server = require('http').createServer(app);
var io = require('socket.io')(server);
// ...
// createServer No 2
http.createServer(app).listen(8000); // opens the port
// Create an HTTPS service identical to the HTTP service.
// createServer No 3
https.createServer(options, app).listen(4433); // for the ssl
You should probably only create one server and add the socket.io call to that one.
If you create only the Port 8000 server, you could add it like this:
// (exactly your code, except for the "var server = " in front.
var server = http.createServer(app).listen(8000); // opens the port
// the next line is new:
var io = require('socket.io')(server);
notice that you'll also have to move the io.on...... part below that line.

Related

Socket IO returning error client side with polling.js

So I have this web application that is am moving to my personal website but I've been encountering a few errors and I haven't been able to find a solution for this one:
For some reason I get
polling.js:311 GET https://kayden.dev/socket.io/?EIO=4&transport=polling&t=O35Ebas 404
on the client side that appears every few seconds.
My code does need some cleaning up so I won't be showing everything but here. The server side code looks something like this:
const http = require('http');
const express = require('express');
const socket = require('socket.io');
const path = require('path');
let app = express();
let port = app.get('port');
let users = [];
let amountOnline = 0;
//send static files from public to the client
let server = app.listen(port, function(){
console.log('listening to requests on port '+ port);
});
//first parameter is the /whatever that goes after the url
app.use("/skyfighters", express.static(__dirname + '/public/'));
//setup socket
let io = socket(server);
io.serveClient(true);
And the client side code only has let socket = io.connect();
that actually executes at the moment with this error happening
Any help is appreciated, this is the first time I migrate a node app the a web server so I'm quite unfamiliar with it.
Thanks!

socketio, client can connect to server if hosted locally, but returns error when trying to connect to server hosted on seperate vserver

I am trying to make a simple server with socket.io and express and connect to it through a website.
when i followed a tutorial on socketio with localhost, everything worked fine, but when i put the server on a vserver, and tried to connect to it, i got this error:
Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR
as well as:
GET https://54.53.0.254:47185/socket.io/?EIO=4&transport=polling&t=O09jjrs net::ERR_SSL_PROTOCOL_ERROR
here is my server code:
const express = require('express');
const app = express();
const server = app.listen(47185);
const socket = require('socket.io');
const io = socket(server)
console.log('server running on port 47185');
io.sockets.on('connection', newConnection);
function newConnection(socket) {
console.log('new connection: ' + socket.id);
socket.on('input', inputLog)
function inputLog(data) {
socket.broadcast.emit('input', data);
console.log(data);
}
}
and here is my client code (this is all that relates to socket.io, the rest is just for the website)
var options = {
rejectUnauthorized:false
}
var socket;
socket = io.connect('89.58.0.199:47185', options);
socket.on('input', foreignInput)
function foreignInput(data) {
terminal_animate('\n' + data)
}
i have tried many different fixes and googled everything i can think of, and i'm just not sure what the problem is.
can anyone help me out with this issue? thanks in advance.
In the documentation, according to the Client Initialization part, in node.js you should provide the protocol when connecting to the server.
// the following forms are similar
const socket = io("https://server-domain.com");
const socket = io("wss://server-domain.com");
const socket = io("server-domain.com"); // only in the browser when the page is served over https (will not work in Node.js)
The first two example shows the secure https/wss as protocol, for that you need to serve the required files from the server, example in the documentation.
With http/ws as protocol it should work, but the communication will not be secure.
The Server Initialization / With Express shows an example to call .listen on the return value of createServer from the http module, with the app given as a parameter.
const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
With a caution that says:
Using app.listen(3000) will not work here, as it creates a new HTTP server.

Can't connect to Socket.io nodejs server

This is the code of the Server:
var http = require('http');
var app = require('express')();
http = http.createServer(app).listen(3400,() => {
var io = require('socket.io')(http);
io.on('connection', (socket) => {
console.log('connection');
});
});
I am using the Socket.io Client Test Tool: https://amritb.github.io/socketio-client-tool/
When I enter the address http://myWANIP:3400 as the URL and hit connect, nothing happens.
What am I doing wrong?
Any help is much appreciated :D
Edit: The Host is behind a NAT and the port 3400 is being forwarded (TCP).
Edit2: After some helpful comments I changed the code to:
var http = require('http');
var app = require('express')();
var server = http.createServer(app).listen(3400);
var io = require('socket.io')(server);
io.on('connection', (socket) => {
console.log('connection');
});
Your second code block looks more appropriate. We can't really tell if your NAT and port forwarding is set up correctly, but if it is, then you should be able to make a socket.io connection from a web page with this:
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io("http://myWANIP:3400", {transports: ["webSocket"]});
</script>
And, when that connection occurs, you should see the results of console.log('connection'); in the server logs.
Another way to verify that your NAT and port forwarding is working correctly is to add this to your server:
app.get("/", (req, res) => {
console.log("got web page request");
res.send("hello");
});
Then, when you got to http://myWANIP:3400, in the browser, you should get a log on your server and a response page back that says "hello".

socket.io can't connect to server

I'm hosting my application on openshift. I am using a custom domain. And socket.io wasn't able to download the client side script so I just used the cdn instead. But now it's not able to connect to a namespace. These are the errors it is giving me on the console log
This is my client side code on the .html page to download the client side script
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
and the .js index page to connect to the index namespace
var socket = io("http://www.loomius.com/index");
Here is my server side code
var express = require('express');
var app = express();
var http = require('http');
var io = require('socket.io')(http);
var mongoose = require('mongoose');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var https = require('https');
// listening on the port
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3002);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
http.createServer(app).listen(app.get('port') ,app.get('ip'), function () {
console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port'));
});
First instead of
var socket = io("http://www.loomius.com/index");
use this to solve issue with a need of cdn
var socket = io.connect("/");
Then on server use this instead
http = http.createServer( app ).listen( process.env.PORT, process.env.IP || "0.0.0.0", function() { // or define ip and port manually
var io = require( 'socket.io' )( http );
io.on('connection', function( socket ) {
// add event listeners here
}
});

Socket.io polling 404 in Laravel

I am trying to implement a chat app with Socket.io
in to my Laravel app. The chat app works fine on it's own,
but I am having problems to make it work in Laravel.
I try to serve Laravel on port 8000 and the chat server on 8000.
I use Express 4.8.0 and Socket.io 1.0.6, Node 0.10.29 and nodemon for testing.
//server.js:
var express = require('express');
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
http.listen(8000, function () {
console.log('listening on *:8000');
});
app.use('/', express.static(__dirname + '/public'));
app.get("/*", function (req, res){
res.sendFile(__dirname + "/index.php");
});
//client.js:
var socket = io.connect('http://localhost:8000');
//html - dependencies, I tried all these:
<script src="//cdn.socket.io/socket.io-1.0.0.js"></script>
{{ HTML::script('/socket.io/socket.io.js') }}
<script src="http://localhost:8000/socket.io/socket.io.js" ></script>
<script src="{{asset('/socket.io/socket.io.js')}}"></script>
and then for the client side (own code)
{{ HTML::script('js/client.js') }}
The CDN version of Socket.io gives constantly these kinds of logs:
"GET http://localhost:8000/socket.io/?EIO=2&transport=polling&t=1407425555977-15 404 (Not Found)".
The others ones just gives a js file not found log:
"GET http://localhost:8000/socket.io/socket.io.js 404 (Not Found)"
//folder structure:
/public
/js
client.js
/node_modules
server.js
Can anyone see what I can do to make it work?
EDIT
//server.js
var socket = require('socket.io');
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = socket.listen(server);
io.on('connection', function (socket) {
console.log("Connected server");
}
server.listen(8000);
//client.js
var socket;
$(document).ready(function () {
socket = io.connect('http://localhost:8000');
});
//When I typ the global "socket" object in the log it says:
connected: false
disconnected: true
This is because you have set it up incorrectly. I had the same exact problem you did (same errors and basic code layout). You need to do npm install socket.io --save while in the base directory of your page (the same as where your index.php file is located). Then you have to do the same for express (npm install express --save). You also have to change your server code. Change the creation of io from:
var express = require('express');
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
To:
var socket = require('socket.io');
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = socket.listen(server);
Then remove the app.use and app.get as they are no longer needed for how this is going to be done. Then add server.listen(8000); at the end of the server.js. For dependencies, use: <script src="//cdn.socket.io/socket.io-1.0.0.js"></script>. Then, to run your server, go to it in terminal and type node server.js. Then just connect to it with your client. Also, for events, in the server, use:
io.on('connection', function (client) {
client.on('someEvent', function(someVariables){
//Do something with someVariables when the client emits 'someEvent'
io.emit('anEventToClients', someData);
});
client.on('anotherEvent', function(someMoreVariables){
//Do more things with someMoreVariables when the client emits 'anotherEvent'
io.emit('anotherEventToClients', someMoreData);
});
});
And in your client code:
socket.emit('someEvent', variables);
socket.on('anEventToClients', function(something){
//Code when anEventToClient is emitted from the server
});

Resources