aws-mqtt
Version:
AWS IoT MQTT broker client
40 lines (36 loc) • 1.56 kB
JavaScript
import MqttClient from 'mqtt/lib/client'
import {signUrl} from './urlSigner'
import MqttWebSocketStream from './streams/WebSocketStream'
import processOptions from './processOptions'
const createStreamBuilder = aws => {
return client => {
const stream = new MqttWebSocketStream(callback => {
// console.log('In webSocketFactory')
signUrl(aws, (err, url) => {
if (err) return callback(err)
// MUST include 'mqtt' in the list of supported protocols.
// See http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718127
// 'mqttv3.1' is still supported, but it is an old informal sub-protocol
// AWS IoT message broker now supports 3.1.1, see https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html
try {
const socket = new window.WebSocket(url, ['mqtt'])
return callback(null, socket)
} catch (err) {
return callback(err, null)
}
})
})
// MQTT.js Client suppresses connection errors (?!), loosing the original error
// This makes it very difficult to debug what went wrong.
// Here we setup a once error handler to propagate stream error to client's error
const propagateConnectionErrors = err => client.emit('error', err)
stream.once('error', propagateConnectionErrors)
return stream
}
}
export default class BrowserClient extends MqttClient {
constructor(options) {
const { aws, mqttOptions } = processOptions(options)
super(createStreamBuilder(aws), mqttOptions)
}
}