Summary
WebSocket connections fail when running under Bun runtime because the ws library's 'upgrade' event is not implemented in Bun's ws polyfill.
Environment
- Runtime: Bun 1.x (
oven/bun:1 Docker image)
- ccxt version: 4.x (latest)
- OS: macOS / Linux (Docker)
Steps to Reproduce
- Install ccxt in a Bun project
- Use any WebSocket-based method (e.g.,
watchTickers, watchOHLCVForSymbols)
- Run with
bun runtime
import ccxt from 'ccxt';
const exchange = new ccxt.pro.binance({ enableRateLimit: true });
const tickers = await exchange.watchTickers(['BTC/USDT']);
Expected Behavior
WebSocket connections should establish successfully and return real-time data.
Actual Behavior
Bun outputs a warning and the connection fails:
[bun] Warning: ws.WebSocket 'upgrade' event is not implemented in bun
NetworkError: Unexpected server response: 101
The same code works correctly under Node.js.
Root Cause
ccxt uses the ws npm package for WebSocket connections. Bun provides a polyfill for ws, but it does not implement the 'upgrade' event (tracked at oven-sh/bun#5951, open since Feb 2026). This causes ccxt's WebSocket error handler to fire, as the HTTP 101 Switching Protocols response is treated as an error.
Suggested Fix
Add runtime detection to use the browser/global WebSocket API when running under Bun, since Bun natively implements the standard WebSocket interface. This could be done in the WebSocket transport layer:
// Detect Bun runtime
const isBun = typeof process !== 'undefined' && process.versions?.bun;
// Use global WebSocket under Bun (native implementation works),
// fall back to 'ws' for Node.js
const WebSocketClass = isBun ? WebSocket : require('ws');
Workaround
Currently, the only workaround is to run ccxt WebSocket workers under Node.js instead of Bun.
Related
Summary
WebSocket connections fail when running under Bun runtime because the
wslibrary's'upgrade'event is not implemented in Bun'swspolyfill.Environment
oven/bun:1Docker image)Steps to Reproduce
watchTickers,watchOHLCVForSymbols)bunruntimeExpected Behavior
WebSocket connections should establish successfully and return real-time data.
Actual Behavior
Bun outputs a warning and the connection fails:
The same code works correctly under Node.js.
Root Cause
ccxt uses the
wsnpm package for WebSocket connections. Bun provides a polyfill forws, but it does not implement the'upgrade'event (tracked at oven-sh/bun#5951, open since Feb 2026). This causes ccxt's WebSocket error handler to fire, as the HTTP 101 Switching Protocols response is treated as an error.Suggested Fix
Add runtime detection to use the browser/global
WebSocketAPI when running under Bun, since Bun natively implements the standardWebSocketinterface. This could be done in the WebSocket transport layer:Workaround
Currently, the only workaround is to run ccxt WebSocket workers under Node.js instead of Bun.
Related
ws.WebSocket 'upgrade' event is not implemented in bun