Closed yungfox closed 2 years ago
Update - turns out the problem was with the ws
library not disposing correctly of the old connection to IoT Hub when the client refreshed the page and opened a new connection... I fixed this using the socket.io
library which handles the client disconnection a lot more easily and allows to properly close and dispose of the old connection to IoT Hub.
io.on('connection', socket => {
client.on('message', message => {
let body = message.getBytes().toString('ascii')
console.log(`new message: ${body}`)
socket.emit('data', body)
client.complete(message)
})
// socket.io can detect client disconnection, this allows the proper disposal of the previous connection
socket.on('disconnect', () => {
console.log('client disconnected')
client.removeAllListeners('message')
})
client.open()
.then(() => console.log('connected to iothub!'))
.catch(() => {
console.log('disconnected from iothub')
client.removeAllListeners('message')
client.close()
})
})
Full working example here
Context
Description of the issue
I am listening for messages from a device on IoT Hub and forwarding them to a Vue.js front-end app through websockets. The connection to the IoT Hub device is istantiated at the connection of the front-end app to the Node back-end, all is working perfectly fine until the front-end page is refreshed, where the connection is established successfully both between IoT Hub and Node and between Node and Vue but the connection to IoT Hub crashes as soon as a new message is received, throwing the exception stack shown below. Not really sure what to do with it, keep in mind I'm not familiar with the library at all but I couldn't find anything regarding this online, hence me opening this issue. I am aware I might be doing something fundamentally wrong with the code, so feel free to tell me so if that's the case.
Code sample exhibiting the issue
Console log of the issue