Registry / messaging / mqtt-websocket-stream

mqtt-websocket-stream

JSON →
library0.1.3jsnpmunverified

A Node.js library that implements a duplex stream over WebSocket, specifically designed to work with MQTT.js. Version 0.1.3 is the latest stable release with no recent updates. Key differentiators: optimized _writev for MQTT.js cork/write/uncork patterns, supports both browser WebSocket and Node.js ws library, and enables async socket creation for signed URLs (e.g., AWS IoT). Alternatives like websocket-stream lack these optimizations.

npm install mqtt-websocket-stream
INSTALL
IMPORT
SIG · MQTT-WEBSOCKET-STR
M
mqtt-websocket-stream
messagingjavascriptv0.1.3
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.

MqttWebSocketStream
import MqttWebSocketStream from 'mqtt-websocket-stream'
const MqttWebSocketStream = require('mqtt-websocket-stream')
ESM default export; CommonJS require() works but TypeScript may need esModuleInterop.
MqttWebSocketStream
const { default: MqttWebSocketStream } = require('mqtt-websocket-stream')
const { MqttWebSocketStream } = require('mqtt-websocket-stream')
CJS named import is wrong; package uses default export only.
WebSocket
import WebSocket from 'ws'
import { WebSocket } from 'ws'
ws package exports default; named export WebSocket works in some versions but is unreliable.

Creates a WebSocket stream for MQTT.js and connects to an MQTT broker with pub/sub.

import MqttWebSocketStream from 'mqtt-websocket-stream'; import WebSocket from 'ws'; import mqtt from 'mqtt'; const wsUrl = 'wss://test.mosquitto.org:8081/mqtt'; const mqttStream = new MqttWebSocketStream(new WebSocket(wsUrl)); const client = mqtt.connect({ stream: () => mqttStream }); client.on('connect', () => { console.log('Connected to MQTT broker'); client.subscribe('test/topic', (err) => { if (!err) client.publish('test/topic', 'Hello mqtt'); }); }); client.on('message', (topic, message) => { console.log('Received:', message.toString()); client.end(); });
Debug
Known issues
gotchaMust provide a WebSocket instance, not a string URL
fix
Always pass a WebSocket object (new WebSocket(url)) instead of the URL string.
affects: >=0.1.0
gotchaAsync factory callback must follow Node.js error-first pattern
fix
callback(null, webSocketInstance) on success; callback(error) on failure.
affects: >=0.1.0
deprecatedNo built-in reconnection support; stream closes on WebSocket close
fix
Implement reconnection logic externally by creating a new stream and reconnecting MQTT client.
affects: >=0.1.0
gotchaBrowser usage requires bundler (e.g., Webpack) to handle Node.js stream module
fix
Configure bundler to polyfill Node.js stream or use browser-specific build.
affects: >=0.1.0
Errors
Common errors & fixes
Error: stream is not a function (or undefined) in MQTT.js connect options
stream option expects a function that returns a stream, not a stream instance directly.
fix
client = mqtt.connect({ stream: () => mqttStream })
TypeError: WebSocket is not a constructor (in Node.js)
Missing ws package or incorrect import.
fix
npm install ws; const WebSocket = require('ws');
TypeError: Cannot read property 'write' of undefined (in async factory)
Async factory not called or callback never invoked.
fix
Ensure the callback is invoked with (null, webSocket) or (error).
Upgrade
Version history
0.1.3latest on npm
Audit
Dependencies
wsoptionalRequired for Node.js WebSocket implementation when not using browser's built-in WebSocket
Agent activity
7 hits · last 30 days
node
4
OpenAI (training)
2
Amazon
1
Resources
mqtt-websocket-stream — npm install mqtt-websocket-stream · libregistry