Anybody knows how to setup a websocket server nodejs (npm package ws) app service on Azure ? My ws client can't connect to the ws server... Thank you for any hint!
Asked
Active
Viewed 1,180 times
1 Answers
1
Seems you missed something to start up your application. I write a sample demo for you,the project secture is simple just like below:
Code of wstest.js
:
const WebSocket = require('ws');
const port = process.env.PORT || 8080
const wss = new WebSocket.Server({ port });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something from server');
});
I use vs code to deploy this project directly, so that this project will under folder : /home/site/wwwroot/
so we should use node command to start up it, just as below:

Stanley Gong
- 11,522
- 1
- 8
- 16
-
1thanks for your reply, it is useful. Apparently it is a SSL problem, although azurewebsites.net has a valid certificate. I just changed from a Free Service Plan(F1) to a Basic Service Plan(P1), now it works with your code. I still have an error with my C++ websocket client, investigating... – Bruce LANE Dec 07 '20 at 10:48
-
@BruceLANE,Glad to know that my solution is helpful. Please click on the check mark beside the answer to toggle it from greyed out to filled in to accept it as an answer, so that it will help others and close this query : ) – Stanley Gong Dec 08 '20 at 01:31
-
@StanleyGong any chance you still have the package.json file with you as I am getting strange errors when client trying to connect to server. I have followed all the steps yo have shown here.. The error : throw er; // Unhandled 'error' event ^ Error: Unexpected server response: 403 at ClientRequest.
(C:\Development\wsazure\node_modules\ws\lib\websocket.js:604:7) Thank you – snowflakes74 Apr 21 '21 at 14:57 -
my package.json contents { "name": "socket-server", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "@types/socket.io": "^2.1.10", "express": "^4.17.1", "socket.io": "^2.3.0", "websocket": "^1.0.34", "ws": "^7.4.5" } } – snowflakes74 Apr 21 '21 at 14:59