WebSocket provider for web3.js 4.x (currently 4.0.8). Part of the web3.js v4 ecosystem, which migrated to ESM-only modules with native TypeScript support. Replaces the HTTP provider for real-time subscriptions (e.g., newBlockHeaders, logs). Key differentiators: supports reconnection with exponential backoff, custom headers, and authentication. Incompatible with web3.js v1.x provider APIs. Published under @ethereum-navigator scope; requires manual subscription management.
npm install web3-providers-wsNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Creates a WebSocket provider with reconnection options and starts a subscription to new block headers.
Use dynamic import: const { WebSocketProvider } = await import('web3-providers-ws') or migrate project to ESM.See the new options type: WebSocketProviderOptions. Pass url as first argument, options object as second.
Use clientConfig.headers: { Authorization: 'Bearer ...' } inside the options object.See quickstart: call provider.connect() or use with web3 instance.
Use web3.eth.subscribe('newBlockHeaders') for parsed events.Use named import: import { WebSocketProvider } from 'web3-providers-ws'Use dynamic import: const { WebSocketProvider } = await import('web3-providers-ws') or set type: 'module' in package.json.Verify the websocket URL (wss://) and ensure the server is running and accessible.