Search code examples
node.jsazurewebsocketws

Client ws can't connect to nodejs websocket server hosted on Azure app service?


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!

azure app service screenshot


Solution

  • Seems you missed something to start up your application. I write a sample demo for you,the project secture is simple just like below: enter image description here

    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:

    enter image description here

    After deploying it, let's test it from local: enter image description here