Replace `XMLHttpRequest` with `fetch`
Is your feature request related to a problem? Please describe.
Currently, socket.io can't be used in Chrome extension background script, since modern Manifest V3 extensions are using service worker as a background script (which doesn't have XMLHttpRequest, only fetch).
Example error:
reason
'xhr poll error'
description
TypeError: xhr.open is not a function
at Request.create (polling.js:250:1)
at new Request (polling.js:237:1)
at Polling.request (polling.js:190:1)
at Polling.doPoll (polling.js:215:1)
at Polling.poll (polling.js:96:1)
at Polling.doOpen (polling.js:56:1)
at Polling.open (transport.js:46:1)
at Socket.open (socket.js:170:1)
at new Socket (socket.js:111:1)
at Manager.open (manager.js:108:1)
Describe the solution you'd like
Replacing XMLHttpRequest with modern fetch.
Describe alternatives you've considered
I think using transports: ['websocket'], would help, but that way I can't use extraHeaders which I use for authorization.
Additional context
"socket.io-client": "^4.7.5",
We could indeed provide another implementation for the HTTP long-polling transport, something like:
import { io } from "socket.io-client";
const socket = io({
transports: [
PollingWithFetch,
WebSocket
]
});
Related: https://github.com/socketio/engine.io-client/issues/716
For future readers:
You can now provide an array of transport implementations:
import { io } from "socket.io-client";
import { Fetch, WebSocket } from "engine.io-client";
const socket = io({
transports: [Fetch, WebSocket]
});
Reference: https://socket.io/docs/v4/client-options/#transports
Implemented in https://github.com/socketio/engine.io-client/commit/b11763beecfe4622867b4dec9d1db77460733ffb and https://github.com/socketio/engine.io-client/commit/f4d898ee9652939a4550a41ac0e8143056154c0a, included in [email protected] and [email protected].