How to make a Server for Websocket secure with node.js
NickName:Paul Ask DateTime:2019-07-07T22:34:20

How to make a Server for Websocket secure with node.js

My websocket server, based on node.js, works for ws:// but not for wss://

The server runs on my Raspberry Pi B 3+. Now that I have changed ws:// to wss:// in my JavaScript file, it does not work anymore.

The node.js server:

const WebSocket = require('ws');
var wss = new WebSoket.Server({ port: 4445 });

wss.on('connection', function connection(ws) {
    console.log("New client connected.");

    ws.on('message', function incoming(data) {
        console.log(data);
        ws.close();
    });

    ws.on('close', function close() {
        console.log("Client disconnected.");
    });

});

The JavaScript client:

var connection = new Websocket('wss://myDomain:4445');

connection.onopen = function () {
    connection.send("Hello");
    connection.close();
}

connection.onerror = function (error) {
    console.log(error);
    connection.lose();
}

'myDomain' is a subdomain that refers to the IP of the Raspberry Pi via dns. I get the following error:

WebSocket connection to 'wss://myDomain:4445/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED

Copyright Notice:Content Author:「Paul」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56923070/how-to-make-a-server-for-websocket-secure-with-node-js

More about “How to make a Server for Websocket secure with node.js” related questions

How to set secure websocket connection using websocket-server and node.js

I can't set secure websocket connection using websocket-server and node.js server side var options = { key: fs.readFileSync(PRIVATE_KEY), cert: fs.readFileSync(CERTIFICATE) }; // create ...

Show Detail

How to make a Server for Websocket secure with node.js

My websocket server, based on node.js, works for ws:// but not for wss:// The server runs on my Raspberry Pi B 3+. Now that I have changed ws:// to wss:// in my JavaScript file, it does not work a...

Show Detail

Create secure websocket server in local machine

I'm trying to use a local websocket server in order to communicate between PWA and windows app. My problem is that pwa requires secure connections and it won't work with a regular websocket server....

Show Detail

How to migrate websocket server to wss (websocket secure)?

I have to build a client-server application that uses websocket secure for communication between the parties. In order to build it, I used this repo as a skeleton: https://github.com/radu-matei/web...

Show Detail

Developing Secure WebSocket Server using C#

I am new to C# programming and have managed to develop non-secure C# websocket server. The server is working as expected with clients connecting to server from browser and c# client application usi...

Show Detail

Node.js Websocket Using Windows Credentials

I am attempting to create a secure websocket connection to a server that uses self-signed SSL certificates. I have added them to my windows machine as trusted certificates, but when I run the node....

Show Detail

Node.js & Socket.io: Self-signed certificates for a secure websocket connection

I've been running across the internet looking for a straight forward answer, but most solutions involve using Express and serving HTTP content for secure connections. I'm more interested in a secur...

Show Detail

How can I implement a secure WebSocket (wss://) server in Python?

I want to serve a real-time stream that has to be securely encrypted due to sensitive data. I've successfully got normal WebSockets streaming using both gevent and gunicorn as direct frontends, bu...

Show Detail

How to Configure WebSocket Server to Accept Secure Connection Request

Before applying ssl(I take from cloudflare) my website is loaded over http and my socket connection is made over ws and it's working fine and connection was made successfully. conn = new WebSock...

Show Detail

Using an existing websocket server with Next.js

I am building an application with Next.js that makes a websocket connection to a Node.js server running on AWS EC2. Since I am hosting the Next.js project on Vercel and am using one of the default ...

Show Detail