How to use WS with Node.js + Cloudflare + Nginx - javascript

I would like to create a websocket using ws.
I use Nginx.
In my Nginx configuration that redirects "gateway.example.com" to "http://127.0.0.1:3006".
In my Cloudflare configuration (DNS), there is gateway to SERVER_IP.
// For "generate" the websocket. There are the Cloudflare key & cert.
const server = require("https").createServer({
cert: require("fs").readFileSync('./src/router/api/v1/cert.pem'),
key: require("fs").readFileSync('./src/router/api/v1/key.pem')
});
server.listen(3006)
const wss = new WebSocket.Server({
server
});

It was a problem with nginx. We need a special configuration for ws (upgrade).

Related

"Where" are Websocket servers hosted on Heroku?

I'm trying to host a Websocket server on my heroku app. There already is a GraphQL server running, but I don't think it's the cause of my problem.
So my server is started like this
const wss = new ws.Server({ port: port }, () => console.log(`Serveur WebSocket prĂȘt ${port}`));
There is no error, but when I try to connect to the server in my browser, just like this :
const ws = new WebSocket('wss://ethyme-api.herokuapp.com/');
I get an error 404.
So my question is, what is the path of the ws server, so I can connect to it ?
Thanks
If your Heroku app is called ethyme-api and and your locally run application is available under ws://localhost:$PORT/ the websocket will be available under wss://ethyme-api.herokuapp.com/ and ws://ethyme-api.herokuapp.com/

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

How to add ssl certificate to websocket to convert the url from ws to wss?

I am trying to make a video call web application using
webrtc and in that i use websocket server in node.js for signaling purpose.
The webrtc application works well in localhost with ws:// websocket url but when i hosted the webrtc application into my server (windows server 2012, IIS ) it required https:// for browser to give access to the camera .
So i added ssl to my website and it is https://.
And i also converted my ws:// url to wss:// url by adding my https:// ssl certificate to websocket server but after that i get the below error when i try to access the websocket url from chrome browser
Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID
This is what i am currently doing in websocket server.
const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');
const server = new https.createServer({
cert: fs.readFileSync('cert.pem'),
key: fs.readFileSync('key.pem')
});
server.listen(8080);
console.log(server.cert);
const wss = new WebSocket.Server({ server });
var users = {};
wss.on('connection', function (connection) {
console.log("User connected");
});
And i access it in my client side javascript like this.
var conn = new WebSocket('wss://server ip address :8080');
conn.onopen = function () {
console.log("Connected to the signaling server");
};
So what part am i doing wrong?
So can i use the same ssl that i bought for my website or should i need to buy new ssl.
And is this the right way to make the websocket url from ws:// to wss:// in node.js
Thanks

Why heroku can't run WebSocket server without HTTP server?

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.

socket.io ssl connection in node.js with express framework

I am quite confused about setting up a secure ssl connection on node.js and the bindings created with the socket.io.
It is giving big headaches, as it is constantly refusing to work, and the result in browsers tested are:
GET https://webrtc.romlex.info/socket.io/1/?t=1405428623632 404 (Not Found) socket error
My serverside code:
var options = {
key: fs.readFileSync(__dirname + '/public/video-phone/key.pem'),
cert: fs.readFileSync(__dirname + '/public/video-phone/cert.pem')
};
actually i am only initializing express
var app = express();
should i create server when initializing express, and pass options?
var app = express.createServer(options);
actually i am creating the https server with the options listening on secured port
var server = require('https').createServer(options,app).listen(443);
which way should i initialize socket.io connection?
Actually i am listening on server with options
var io = require('socket.io').listen(server,options);
Should i listen on server only?
var io = require('socket.io').listen(server);
Or should i listen on the secure port and options?
var io = require('socket.io').listen(443,options);
Quite a bit confused, please help!
i am using xhr-polling over websockets
io.set('transports', [
//'websocket',
'xhr-polling',
'jsonp-polling'
]);
the serverside code:
serverside socket.io with ssl
clientside code:
should i add secure param or not?
io.connect("https://webrtc.romlex.info", {secure: true});
Sorry for the questions, i'm quite a newbie on node.js, especially, when ssl connection comes to scene.
Thanks for your suggestions!

Categories

Resources