Search code examples
node.jswebsocketmqttmosca

Mosca MQTT - Client instantly unsuscribed


On the server I am listening to subscriptions and logging them

mqttServ.on('subscribed', (topic, client) => {
  mqttServ.publish({
    topic: topic,
    payload: `client ${client.id} subscribed`,
    qos: 1
  }, client)
})

mqttServ.on('unsubscribed', (topic, client) => {
  console.log(
    `client ${client.id} unsuscribed from topic ${topic}`
  )
})

On the client I get correctly the first message

client.on('connect', () => {
  client.subscribe('goodmorning')
})

client.on('message', (topic, payload) => {
  console.log([topic, payload].join(": "))
  client.end()
})

But I am unable to send others... probably because I notice that the clients are getting unsuscribed for the topic. Why the unsubscription is happening?


Solution

  • Remove (or comment out) the client.end() call - this closes the client's connection to the broker, which will at the server result in the subscription being removed because the client is no longer connected :-o