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

[Browser] Doesn't switch to polling when WebSocket is undefined. #5084

Closed
hassansin opened this issue Nov 16, 2023 · 1 comment
Closed

[Browser] Doesn't switch to polling when WebSocket is undefined. #5084

hassansin opened this issue Nov 16, 2023 · 1 comment
Labels
bug Something isn't working package:engine.io-client This concerns the "engine.io-client" package

Comments

@hassansin
Copy link

hassansin commented Nov 16, 2023

I have set my transports to use WebSocket first and then fallback to polling e.g. transports: ["websocket", "polling"].

To test if the fallback transport really works, I used TamperMonkey browser extension to disable Websocket by setting WebSocket = void 0.

Instead of switching to polling, I keep getting connect_error with the message timeout from websocket transport.

Tried to investigate and it looks like transport switching only works if createTransport method throws here:

https://github.com/socketio/engine.io-client/blob/fa479164251dd1f283dd163143b546582161339a/lib/socket.ts#L467-L474

But the method always returns a transport wrapper class, even when WebSocket is not defined.

To Reproduce

Engine.IO server version: 6.5.2

Server

const engine = require("engine.io");
const server = engine.listen(3000, {
  transports: ["polling", "websocket"]
});

server.on("connection", (socket) => {
  console.log("connection");

  socket.on("message", (data) => {
    console.log("data", data);
  });

  socket.on("close", () => {
    console.log("close");
  });
});

Engine.IO client version: 6.5.2

Client

const socket = require("engine.io-client")("ws://localhost:3000", {
transports: ["websocket", "polling"]
});

socket.on("open", () => {
  console.log("open");

  socket.on("message", (data) => {
    console.log("data", data);
  });

  socket.on("close", () => {
    console.log("close");
  });
});

Expected behavior

Should be able to fallback to polling transport when WebSocket transport is disabled on the browser.

Platform:

  • Device: Chrome 119.0.6045
  • OS: Windows

Additional context
Add any other context about the problem here.

@hassansin hassansin added the bug Something isn't working label Nov 16, 2023
darrachequesne referenced this issue in socketio/engine.io-client 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:

- https://github.com/socketio/engine.io-client/issues/575
- https://github.com/socketio/socket.io-client/issues/1448

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

Related:

- https://github.com/socketio/engine.io-client/issues/714
- https://github.com/socketio/socket.io-client/issues/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.
@darrachequesne darrachequesne transferred this issue from socketio/engine.io-client Jul 9, 2024
@darrachequesne darrachequesne added the package:engine.io-client This concerns the "engine.io-client" package label Jul 9, 2024
@darrachequesne
Copy link
Member

For future readers:

The tryAllTransports option let the client test all transports instead of just the first one:

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

const socket = io({
  tryAllTransports: true
});

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

Implemented in socketio/engine.io-client@579b243, included in [email protected] and [email protected].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working package:engine.io-client This concerns the "engine.io-client" package
Projects
None yet
Development

No branches or pull requests

2 participants