Socket.io doesn't work in production - javascript

I have tried socket.io on localhost and it works perfectly. However, when in production I get the following error:
GET http://77.235.46.164:3000/socket.io/?EIO=3&transport=polling&t=1448754369321-21 net::ERR_CONNECTION_TIMED_OUT
On the server side I have the following code:
var express = require('express');
var mysql = require('mysql');
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
connection.connect();
server.listen(process.env.PORT || 3000);
While on the client side:
var socket = io.connect('http://77.235.46.164:3000'); .... etcs
I am also starting the node server (I have VPS account in EuroVPS).
I have searched everywhere but I don't seem to find a solution that works for me.

Is it working without firewall?
In some controlled network Firewall may block websockets & ports other than 80,443.

You can create an HTTP server yourself,instead of having the Express framework create one for you, so you will be able to reuse your HTTP server, in order to keep the same server instance.
You can try :
var express = require('express');
var app = express();
//Create your server
var server = app.listen(3000);
var io = require('socket.io')(server);
//Then you can listen for connection
io.on('connection', function(socket){
socket.emit('an event sent to all connected clients')
//Awesome things
})

Related

How to convert local server to online server Node.js

I've been developing this game for a school project. It is supposed to be online multiplayer, but this far i have online used it locally. I can figure out how to change my server code for it to be able to act as a "real" online server.
The server code:
// Dependencies
var express = require('express');
var http = require('http');
var path = require('path');
var socketIO = require('socket.io');
var app = express();
var server = http.Server(app);
app.set('port', 5000);
app.use('/static', express.static(__dirname + '/static'));
// Routing
app.get('/', function(request, response)
{
response.sendFile(path.join(__dirname, 'index.html'));
});
// Starts the server.
server.listen(5000, function()
{
console.log('Starting server on port 5000');
});
// Add the WebSocket handlers
io.on('connection', function(socket)
{
console.log('New player arrived');
});
Would appreciate any help greatly.
Most of the shared servers run at port 8080
so you may change the port then upload it into the hosting, If you just need it to be live u can use https://heroku.com/ so u can deploy it there
also here's quick option
npm install -g localtunnel
lt --port 8000
You will receive a URL, for example, https://school.localtunnel.me, that you can share with anyone for as long as your local instance of lt remains active. Any requests will be routed to your local service at the specified port
For more info: https://localtunnel.github.io/www/

why to make new instance of http for Socket.io when we already have express server?

I am new to SocketIO, I have referred many blogs and documentation for socket and everywhere we first need to create an HTTP server and then attach the socket to it like this -
var app = express();
var httpServer = http.createServer(app);
var io = socketio.listen(httpServer);
What does the second line mean? why are we creating one extra HTTP server while express(web framework) is already defined?
Because I never created a new HTTP instance for my RESTful application, I simply listened to express instance like this -
var express = require('express');
var app = express();
app.listen(8000);
Thanks in advance!
If you want socket.io to run on the same port as your web server, then you use the same server instance. If you want socket.io to run on a different port, then you create a new server instance on that port just for socket.io to use.
Socket.io works just fine using the same port and server instance as Express so unless you have a specific reason to run it on a different port, this is the usual way one would configure it.
Some code examples for socket.io show it in isolation by itself and thus they have to create an http server for it to use.
When using Express, you can get the server instance like this:
const express = require('express');
const socketio = require('socket.io');
const app = express();
const server = app.listen(8000);
const io = socketio(server);

node.js server external access not working

I have set up a node.js server that is supposed to serve HTML files from a directory to clients. It should also log any connecting and disconnecting clients. Both work perfectly, but only locally. I have already forwarded traffic on port 3000 to my server and deactivated its firewall. What else can I do to enable external access?
var express = require('express');
var socket = require('socket.io');
//app setup
var app = express();
var server = app.listen(3000, ready);
function ready(){
console.log('setup completed\nlistening on port 3000\n\n');
}
app.use(express.static('public'));
//socket setup
var io = socket(server);
io.on('connection', newConnection);
function newConnection(socket){
//some code
}
i have already forwarded traffic on port 3000 to my server
Thats the wrong way round. Your server listens on port 3000, but webservers offer there service under Port 80, so you have to forward Port 80 to Port 3000.
and deactivated its firewall.
Bad idea. Just open one port (80 or 3000 depending on where you do port forwarding). And usually Routers on the way will also block certain ingoing connections, so check that too.

Express generator with socket.io configuration

Below code is my current configuration, it worked but I'm confused.
server side
var server = require("http").Server(express);
var io = require("socket.io")(server);
server.listen(5000);
io.on('connection', function(client) {
});
cilent
var socket = io.connect('http://localhost:5000');
Why we need to create another server for socket at port 5000 for an application? can't socket use 3000? which is the express's running port. I removed the line of server.listen('5000') and do server.listen() and try connect to port 3000 at client side it doesn't work.
By default express 4 create a server and run it bin/www. I solved this issue by commenting out server.listen(port); in that file.

Express.js with websockets

Currently my application based on Expressjs + angularjs. I want to start few 2 way calls along with existing http calls. I went through few websocket chat tutorials but nonew of them integrated with expressjs.
Do I start websocket connection on new port? How do I integrate my angularjs with websocket?
Can I just create few more routes and controller functions and have some of them work 2 way?
Nothing special is needed, you can use the same port for Socket.IO and express.
e.g. in my project I do something like this:
var express = require('express');
var io = require('socket.io');
var app = express();
var server = http.createServer(app).listen(SERVER_PORT, function() {
console.log('Express server listening on port ' + SERVER_PORT);
});
// let socket.IO listen on the server
io = io.listen(server);
io.on('connection', function(socket) { /* handle connection */ });
AFAIK there is also an example with express on the Socket.IO wiki.

Categories

Resources