How to convert local server to online server Node.js - javascript

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/

Related

Node.js + Apache - https://localhost:3000/socket.io/ ERR_CONNECTION_REFUSED

I am implementing a simple game in Node.js. I have a client.js for my client side code, and a server.js running on a remote server, which both use sockets to communicate on port 3000
I am also running Apache on port 80, and using ProxyPass in my apache configuration file, to route the url mywebsite.io/agario to my nodejs server.
<Location /agario>
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
I am also using cloudflare to route my webserver 167.179.xx.xx through the url https://agario.mywebsite.io for SSL so that I can use HTTPS.
The problem
When I try to connect to my website https://agario.mywebsite.io/agario I am receiving the following error:
socket.io-1.4.5.js:1 GET https://localhost:3000/socket.io/?EIO=3&transport=polling&t=MakAMgZ net::ERR_CONNECTION_REFUSED
I am unclear why my client code is trying to connect to localhost, when I have specified in the code to connect to the remote server. Potentially I am just confused on how to run the node.js server as this is my first taste of Node.js and sockets.
client.js
...
var socket;
socket = io.connect('https://agario.mywebsite.io/agario');
...
server.js
var app = express();
var server = app.listen(3000, listen);
// This call back just tells us that the server has started
function listen() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://' + host + ':' + port);
}
app.use(express.static('public'));
var io = require('socket.io')(server);
io.sockets.on('connection',
function(socket) {
console.log("We have a new client: " + socket.id);
...
});
If I have missed out any vital information please let me know and I will update my question, thank you.
You server is listening on port 3000 and you're trying to connect with it via 443, you should try something like this
socket.connect('https://ip:3000');
However, if you're sure that ur client is using the same port as the server or u have a port forwarding then try to use netcat just to make sure the the problem is with your script not the network config :
nc -zv -w1 ip port

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.

Socket.io doesn't work in production

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
})

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