Real Time Communication With MQTT – It’s in Trend (Part – 2)


My previous blog was all about server (broker) creation and options, provided during creation. In this blog post, I am going to discuss available events of Mosca and the step by step procedure of creating MQTT client.

Let’s start with Mosca events first. Mosca emits these events for the server.

  1. clientConnected :  when a client is connected the client is passed as a parameter.
    ex:  server.on( ‘ clientConnected ‘ , callback);
  2. clientDisconnecting :  when a client is being disconnected the client is passed as a parameter.
    ex:  server.on( ‘ clientDisconnecting ‘, callback);
  3. clientDisconnected :   when a client is disconnected the client is passed as a parameter.
                                    ex:  server.on( ‘ clientDisconnected ‘, callback);
  4. published :   when a new message has published the packet and the client are passed as parameters.
    ex:  server.on(‘published’, callback).
    Here you will receive ‘ packet ’ and ‘ client’ as arguments of the callback function. The client will contain the client object from where the message is published from and the packet object contains the details of the message/packet that is sent by the client.

    • topic :   topic name where the message is published.
    • payload :   The payload property contains the message as a buffer of bytes. To get the readable string format convert it to UTF-8. i.e.
    • messageId :   it’s a random unique Id .
    • QoS :   QoS is 0 by default ,you can set it to 1 for the guarantee of service.
    • retain :   retain is also by default false. But if you set it to true, this message will be treated a bit differently.In that case, the message will be stored and will be published by the broker whenever a new client will subscribe to the same topic the retain message has (It’s like a welcome  message ) . But please remember only the latest retain message will be stored per topic.

  5. subscribed :   when a client is subscribed to a topic the topic and the client are passed as parameters.
    ex: server.on( ‘ subscribed ‘, callback);
  6. unsubscribed :  when a client is unsubscribed to a topic,the topic and the client are passed as parameters.
    ex: server.on( ‘ unsubscribed ‘, callback);
  7. ready :   when the server is ready.
    ex: server.on( ‘ ready ‘, callback);
  8. error :   when the server has thrown an error.
    ex: server.on( ‘ error ‘, callback);

As we have created our server already, now we need to create a client to test whether we have successfully implemented the MQTT client – server application.

Step 1 : install MQTT using npm

Step 2: create a client_one.js file and load the MQTT module.

Step 3: now connect your client with MQTT server.

The connect function takes two arguments host URL and options. Protocol can be mqtt://, ws:// mqtts:// and wss:// (ws stand for WebSocket). For secure connection provide details in the options argument.

Step 4 :  now subscribe to a topic.

Step 5:  after subscription now let’s publish a message.

Step 6: now create an another client in client_two.js file with above-mentioned procedure. Now we are going to listen on ‘topic/child’ channel. MQTT emits message event whenever there is message available in a channel.

Step 7:  run server.js , client_one.js , client_two.js in separate tabs using node Js.

Step 8: To unsubscribe from a topic.

To use mqtt.js from browser just download mqtt.js and implement it in this way.

Hurray !!!!!  we have successfully implemented the MQTT  client application .For more details and example, visit my GitHub Page.

Subhajit Ghosh

Subhajit Ghosh is a senior software engineer at Innofied Solution Pvt. Ltd. He loves to cook but his love for cooking codes is far more than that. He is a codeworm , loves to experiment his coding skills in different languages, keen in taking part at dev fests and summits. He loves what he does, and he does what he loves…and that’s coding.