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
Related
Serving a vue js + socket io client from a node js server (with socket io obviously)
while developing i was connecting and awaiting calls from localhost:8000
new Websocket(
new Server(server, {
cors: {
origin: "http://localhost:8000",
methods: ["GET", "POST"],
},
}),
);
but now, obviously, there is no longer localhost:8000 and I am serving my client's dist/index.html through my webserver as a static file
app.use(express.static(path.join(__dirname, "../../client/dist")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "../../client/dist/index.html"));
});
I feel like I am missing something really really simple? am I supposed to host my client on a different "web server" if i want to use the socket.io functionality?
as in create a simple web server for the client - and serve it from there on port 8000? and run my own server seperatly ? on port 3000?
I'm hosting this on an EC2 container on aws
I've figured it out - I need to connect to the machine's ip instead of localhost through the client's app - cause localhost:3000 doesn't actually exist...otherwise everything else is ok
When I enter www.example.com:3000 in the browser, I receive this error (where 'example' is the name of my domain)
This site can't be reached - www.example.com took too long to respond.
I have done these things:
Installed node.js on my GoDaddy shared account
Created a folder ../public_html/testsite
Placed two files in that folder: app.js and .htaccess.
Start webserver with: node app.js
Go to browser and enter my domain's URL and port:
Receive the error message above
This post is very helpful, but I still cannot get my set up to work.
These two files are in ../public_html/testsite/
.htaccess
RewriteEngine on
RewriteRule ^index.html.var$ http://www.example.com:3000/$1 [L,P,QSA]
RewriteRule (.*) http://www.example.com:3000/$1 [P,L]
Note: index.html is the file that normally loads when you visit here
The app.js:
const http = require('http');
const hostname= '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('NodeJS server running on Shared Hosting\n');
});
server.listen(port, hostname, () => {
console.log('Server running at http://' + hostname + ':' + port + '/');
});
To start the webserver:
> cd ../public_html/testsite
> node app.js
Server running at http://127.0.0.1:3000
In browser, I enter:
www.example.com:3000
I expected to see, in the browser
NodeJS server running on Shared Hosting
Questions:
Should I use my own godaddy domain address for hostname in .htaccess or should it be localhost?
Is the idea to redirect www.example.com:3000 (in browser) to http://localhost:3000 (on GoDaddy server)?
I've tried all sorts of permutations (using my ip address or domain name, different port numbers, etc.)
I think I am close, but need a few ideas to try!
Port 3000, 8080, and the likes are normally used for development purposes, as in development in can be useful to have several servers running at the same time, for example one on port 3000, one on port 3001, etc.
However, on the internet, HTTP is served on port 80 and HTTPS is served on port 443. So basically, in your server implementation, you should set the port dynamically: it must not be the same whether you're running in production and in development!
I personally use the fact that on my production environment (ie for you, GoDaddy's deployment machines), the environment variable PORT is already set to 80, whereas on my local machine I don't set it, so I can write this:
const express = require('express');
const port = process.env.PORT || 3000; // 3000 on my machine, 80 on GoDaddy's server
const app = express();
app.listen(port, () => console.log(`App listening on port ${port}`));
And I access the server at these URLs:
http://localhost:3000/
http://example.com:80/
http://example.com/
The last two are the same because, as previously said, default HTTP port is 80.
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.
I have a WebSocket app on heroku. I tried uploading my WebSocket app without routing http server but it doesn't work. What does mean server routing? And why can't heroku run a WebSocket server without an http server?
And why does it takes express object 'server' as argument in SocketServer
Here's my code of Websocket server.
const express = require('express');
const SocketServer = require('ws').Server;
const path = require('path');
const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');
const server = express()
.use((req, res) => res.sendFile(INDEX) )
.listen(PORT, () => console.log(`Listening on ${ PORT }`));
const wss = new SocketServer({ server });
wss.on("connection",function(ws){
ws.on("message",function(message){
if(message==='exit'){
ws.close();
}else{
wss.clients.forEach(function(client){
client.send(message);
});
console.log(message);
}
});
ws.send("welcome..");
});
There are too many questions in one. I hope my answers will address all of them.
First of all, the websocket protocol is an extension of http, it is not something different. Therefore, a websocket server is necessarily an http server, even though with extended capabilities.
Also, with socket.io, the websockets and http run on the same port. Therefore you have to open the port for http to have your websockets work.
Let's have a look at this line :
const wss = new SocketServer({ server });
It has a syntax error, it must be new SocketServer(server). Also, about the variable name, I would recommend not to use wss for a websocket over http, since wss stands for secure websockets and is related to websockets like http to https.
About routing in heroku: in heroku, you have to define which port your application is using. This can be compared to the firewall on your local machine : if you want to access it from outside, you have to open the port. On heroku, technically it is different to a firewall, but in this point it is similar.
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.