mqtt-websocket-stream
v0.1.3
Published
Binary WebSocket stream for MQTT.js
Downloads
27
Readme
mqtt-websocket-stream
This node module implements MQTT.js compatible duplex stream over WebSocket. It is an alternative implementation to websocket-stream that is better suited to work with MQTT.js. This module is used on aws-mqtt to connect to AWS IoT MQTT broker.
Features
- The stream implements
_writev
to efficiently write multiple small buffers that MQTT.js writes to the stream (MQTT.js callscork
, multiplewrite
and thenuncork
on the stream to send one packet) - Can work with browser's WebSocket, as well as server WebSocket implementation (ws)
- Supports async socket creation, when instantiation of the WebStream needs to be delayed/async, for example to async sign the url to connect to.
Usage
npm install mqtt-websocket-stream --save
In node environment:
import MqttWebSocketStream from 'mqtt-websocket-stream'
import WebSocket from 'ws'
const stream = new MqttWebSocketStream(new WebSocket(url))
stream.pipe(...).pipe(stream)
In browser environment (using Babel/Webpack):
import MqttWebSocketStream from 'mqtt-websocket-stream'
const stream = new MqttWebSocketStream(new window.WebSocket(url))
stream.pipe(...).pipe(stream)
Passing an async factory:
import MqttWebSocketStream from 'mqtt-websocket-stream'
const stream = new MqttWebSocketStream((callback) => {
getSignedUrl().then(url => callback(null, new window.WebSocket(url))).catch(callback)
})
stream.pipe(...).pipe(stream)