javascriptsocket.iosocket.io-client

Socket.io-client connection error: websocket error


Previously, I was getting the output "xhr poll error" with the code:

import { io } from "socket.io-client";

const socket = io("https://socket.io/docs/v4", {
    reconnection: true,
    reconnectionDelay: 1000,
    reconnectionDelayMax: 5000,
    reconnectionAttempts: 3
});

socket.on('connect_error', function(error) {
    console.log(error.message);
});

With a bit of research, this was "resolved" by adding "transports: ['websocket']". Here's my current code:

import { io } from "socket.io-client";

const socket = io("https://socket.io/docs/v4", {
    reconnection: true,
    reconnectionDelay: 1000,
    reconnectionDelayMax: 5000,
    reconnectionAttempts: 3,
    transports: ['websocket']
});

socket.on('connect_error', function(error) {
    console.log(error.message);
});

Unfortunately, this doesn't really resolve my problem because now I get the following output: websocket error.

Other things I've tried: specifying the port after the URL and setting rejectUnauthorized to false. (both giving me the same websocket error.

I'm a bit stumped on what to do next, and it'd be nice to hear some of you guys' feedback!

Socket.io-client version: 4.5.1


Solution

  • By default, the client will try to establish a WebSocket connection if possible and will fall back on HTTP long polling if not, which explains why the first change you mentioned resolved the polling error, but now you have a websocket error.

    If it's attempting to reconnect automatically, you might need to enable CORS or set credentials. If it's not, you should try manually reconnecting after a timeout with socket.connect()