Can not connect to socket.io with websocket

繁伸 松尾 51 Reputation points
2021-03-04T00:41:13.293+00:00

Hi,

This is my Azure NodeJS AppService's socket.io setup

Client side:

const socket = io('https://testappsocket.azurewebsites.net', {
  withCredentials: true,
  forceNew: true,
  reconnectionAttempts: "Infinity", //avoid having user reconnect manually in order to prevent dead clients after a server restart
  timeout: 10000, //before connect_error and connect_timeout are emitted.
  transports: ['websocket']
});
socket.on("notify", (msg) => {
      console.log("getting socket msg");
      console.log(msg.message);
      this.get_socket_message = msg.message;
});

Server side:

var io = require('socket.io')(server, {
  cors: {
    origin: "https://testappsocket.azurewebsites.net",
    methods: ["GET", "POST"],
    credentials: true,
  },
  transports: ['websocket']
});
console.log("getSocekt.js : ")
console.log(msg)
var rtnMessage = { message: msg };
io.emit('notify', rtnMessage);

But my client can not receive message from server or occasionally it can only receive first message, from the second onwards the "WebSocket is closed before closed before the connection is established" error message always shown.

I am using Socket.io version 3.1.0 with NodeJS 14.15.1

How can I fix this ? Thank you very much

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,865 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Qua628 1 Reputation point
    2021-07-04T13:55:25.387+00:00

    hi @繁伸 松尾 have you solved your issue? I'm in a similar situation (standalone, no Azure) with same results as you...