Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Won't switch to polling transport when Websocket transport failed #1599

Open
hassansin opened this issue Dec 8, 2023 · 3 comments
Open

Won't switch to polling transport when Websocket transport failed #1599

hassansin opened this issue Dec 8, 2023 · 3 comments
Labels
question Further information is requested

Comments

@hassansin
Copy link

Describe the bug
I'm trying to connect with websocket transport first and if it fails I was hoping socketio would switch to long-polling. But for some reason, it keeps reconnecting with websocket transport and never switches to polling.

image

To Reproduce

Socket.IO server version: 4.7.2

Server

const { Server } = require("socket.io");
const express = require("express");
const http = require("http");

const server = http.createServer(express());
const io = new Server({
	transports: ["polling", "websocket"],
});
io.on("connection", socket => {
	console.log("New Connection");
});

io.attach(server);
server.listen(3000);

Socket.IO client version: 4.7.2

Client

import { io } from "socket.io-client";
const socket = io({
    transports: ["websocket", "polling"],
});
socket.on("connect_error", err => {
    console.log("connect_error", err);
})

Expected behavior
Was expecting the transport would switch to long-polling when websocket isn't available.

Platform:

  • Device: Firefox 120
  • OS: Windows 10

Additional context
I disabled websocket by blocking websocket calls using the Firefox request blocking feature. Can be disabled using this TamperMonkey script as well.

@hassansin hassansin added the to triage Waiting to be triaged by a member of the team label Dec 8, 2023
@darrachequesne
Copy link
Member

Hi!

The order in the transports option matters:

const socket = io("https://example.com", {
  transports: ["websocket", "polling"] // use WebSocket first, if available
});

socket.on("connect_error", () => {
  // revert to classic upgrade
  socket.io.opts.transports = ["polling", "websocket"];
});

Reference: https://socket.io/docs/v4/client-options/#transports

@darrachequesne darrachequesne added question Further information is requested and removed to triage Waiting to be triaged by a member of the team labels Dec 8, 2023
@hassansin
Copy link
Author

@darrachequesne okay, so if Websocket is blocked by proxy and unable to connect, shouldn't it switch to polling?

@hassansin
Copy link
Author

Anything? What's the point of having multiple transports if automatic switching doesn't work?

darrachequesne added a commit to socketio/engine.io-client that referenced this issue May 28, 2024
When setting the `tryAllTransports` option to `true`, if the first
transport (usually, HTTP long-polling) fails, then the other transports
will be tested too.

This is useful in two cases:

> when HTTP long-polling is disabled on the server, or if CORS fails

Related:

- #575
- socketio/socket.io-client#1448

> when WebSocket is tested first (`transports: ["websocket", "polling"])

Related:

- #714
- socketio/socket.io-client#1599

The only potential downside is that the connection attempt could take
more time in case of failure, as there have been reports of WebSocket
connection errors taking several seconds before being detected (that's
one reason for using HTTP long-polling first). That's why the option
defaults to `false` for now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants