Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io v3 Unsupported protocol version error

I'm stuck with the newer version of socket.io. Everything was fine but when I wanted to upgrade to socket.io 3, everything just broke, and currently on the client, I'm getting a 400 HTTP status code with the following JSON response - {"code":5,"message":"Unsupported protocol version"}

Server-side config -

const io = require("socket.io")(server, {
    cors: {
      origin: config.clientURL,
      methods: ["GET", "POST"],
      credentials: true,
    },
  });

Client-side config -

const socket = io(backendURL, {
  withCredentials: true,
});

I've tried very many things and redeployed many times but the error didn't go away.

For reference, I've these github repos -

Client in react.js - GitHub repo

Server in nodeJs and socket.io.js - GitHub repo

like image 771
Piyush Avatar asked Feb 04 '21 13:02

Piyush


People also ask

Why is Socket.IO not connecting?

Problem: the socket is not able to connect​You are trying to reach a plain WebSocket server. The server is not reachable. The client is not compatible with the version of the server. The server does not send the necessary CORS headers.

Can we use Socket.IO in Android?

Installing the Dependencies​Now we can use Socket.IO on Android!

Does Socket.IO need HTTP?

Websocket is created when you make upgrade from http to websocket, so it kind of does need http. socket.io isn't a pure Websocket server/implementation, it depends on HTTP for its initial connection setup.


1 Answers

Looks like there may be a mismatch between versions of your socket.io-client and socket.io server.

First, update the servers with allowEIO3 set to true (added in [email protected])

const io = require("socket.io")({
  allowEIO3: true // false by default
});

After you've upgraded the socket.io-client (latest right now is 3.1.1), you can set it back, or remove it since default is false

const io = require("socket.io")({
  allowEIO3: false
});
like image 75
Zeehad Avatar answered Oct 22 '22 23:10

Zeehad