Registry / messaging / bilibili-live-danmaku

bilibili-live-danmaku

JSON →
library0.7.16jsnpmunverified

A TypeScript-friendly WebSocket client for Bilibili live danmaku (bullet comments) and room events. Version 0.7.16 supports Node.js and browser environments with ESM and CJS builds. Provides LiveWS and KeepLiveWS classes for connecting to Bilibili live rooms, automatically handling heartbeats, reconnection, and message parsing. It decodes zlib (protover=2) and brotli (protover=3) payloads, emits typed events for commands like DANMU_MSG, SEND_GIFT, and HEARTBEAT_REPLY. Compared to alternatives like bilibili-live-ws, this library modernizes the API using standard WebSocket and EventTarget patterns, includes TypeScript definitions, and supports custom auth for open-live.bilibili.com. Released under MIT license with monthlyish updates.

npm install bilibili-live-danmaku
INSTALL
IMPORT
SIG · BILIBILI-LIVE-DANM
B
bilibili-live-danmaku
messagingjavascriptv0.7.16
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LiveWS
import { LiveWS } from 'bilibili-live-danmaku'
const LiveWS = require('bilibili-live-danmaku')
Named export, not default. ESM-only in modern Node; CJS require() works only with bundler or node --experimental-require-module.
KeepLiveWS
import { KeepLiveWS } from 'bilibili-live-danmaku'
import KeepLiveWS from 'bilibili-live-danmaku'
Not a default export. KeepLiveWS auto-reconnects; LiveWS does not.
getLiveConfig
import { getLiveConfig } from 'bilibili-live-danmaku'
Utility to fetch room config (host, key, etc). Async function returning a Promise.

Connects to Bilibili room 14327465 using LiveWS, listens for DANMU_MSG and SEND_GIFT events.

import { LiveWS } from 'bilibili-live-danmaku'; const roomid = 14327465; // Bilibili room number const live = new LiveWS(roomid, { protover: 2, // 1: no compress, 2: zlib, 3: brotli uid: 0, // optional uid key: '', // optional token buvid: '' // optional buvid }); live.addEventListener('open', () => { console.log('WebSocket connected'); }); live.addEventListener('CONNECT_SUCCESS', () => { console.log('Joined room successfully'); live.addEventListener('DANMU_MSG', (e) => { const { info } = e.data; console.log('Danmu:', info[1]); // the text }); live.addEventListener('SEND_GIFT', (e) => { console.log('Gift:', e.data.data.giftName); }); live.addEventListener('HEARTBEAT_REPLY', (e) => { console.log('Popularity:', e.data); // number }); }); live.addEventListener('close', () => { console.log('Connection closed'); }); live.addEventListener('error', (err) => { console.error('WebSocket error:', err); }); // To stop: // live.close();
Debug
Known issues
breakingEvent names changed from lowercase (e.g., 'message') to uppercase (e.g., 'MESSAGE') in v0.6.0
fix
Use addEventListener with uppercase event names: 'CONNECT_SUCCESS', 'HEARTBEAT_REPLY', 'MESSAGE', 'DANMU_MSG', etc.
affects: <0.6.0
breakingprotover=3 (brotli decompression) requires Node.js >=10.16 or browser with Brotli support. Older Node will throw error.
fix
Set protover to 2 (zlib) if environment doesn't support Brotli, or use a polyfill.
affects: >=0.4.0
deprecatedThe method live.on() is deprecated in favor of addEventListener()
fix
Replace live.on('DANMU_MSG', callback) with live.addEventListener('DANMU_MSG', callback).
affects: >=0.6.0
gotchaKeepLiveWS constructor does not accept the same options as LiveWS; it overrides roomid and some defaults.
fix
Check KeepLiveWS signature: new KeepLiveWS(roomid, options) but options.authBody is used instead of individual fields.
affects: >=0.7.0
gotchaWhen using authBody option, it overrides roomid, key, uid, buvid, protover. If authBody is an object, it's encoded as JSON; if string/Uint8Array, sent raw.
fix
Ensure authBody matches expected format; roomid in authBody takes precedence.
affects: >=0.7.0
gotchaThe library uses global WebSocket; in Node.js versions <18, you may need a polyfill like 'ws'.
fix
Install 'ws' and set global.WebSocket = require('ws') before importing the library.
affects: <18
Errors
Common errors & fixes
Uncaught ReferenceError: WebSocket is not defined
Running in Node.js <18 without a WebSocket polyfill.
fix
Install 'ws' and set global.WebSocket = require('ws'); before importing the library.
Cannot find module 'bilibili-live-danmaku' or its corresponding type declarations.
Missing npm install or incorrect import path.
fix
Run 'npm install bilibili-live-danmaku' and use correct import: import { LiveWS } from 'bilibili-live-danmaku'. Ensure tsconfig has esModuleInterop or moduleResolution node.
TypeError: live.addEventListener is not a function
Importing LiveWS incorrectly as default export instead of named export.
fix
Use import { LiveWS } from 'bilibili-live-danmaku' instead of import LiveWS from 'bilibili-live-danmaku'.
Upgrade
Version history
0.7.16latest on npm
Audit
Dependencies
bufferoptionalRequired for Uint8Array/Buffer operations in non-Node environments; polyfill included for browsers.
pakooptionalUsed for zlib decompression when protover=2. Optional if protover not set to 2.
Agent activity
33 hits · last 30 days
node
30
OpenAI (training)
2
Resources
bilibili-live-danmaku — npm install bilibili-live-danmaku · libregistry