Skip to content

WebSocket connections fail under Bun runtime: ws 'upgrade' event not implemented #28494

@phpmac

Description

@phpmac

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

  1. Install ccxt in a Bun project
  2. Use any WebSocket-based method (e.g., watchTickers, watchOHLCVForSymbols)
  3. 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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions