Create Your Own IM Social App with MQTT

Image for post
Image for post

Introduction

Message Queue Telemetry Transport (MQTT) is an instant communication protocol based on the publish-subscribe pattern. It is lightweight, open source, easy-to-use, and supports Quality of Service (QOS). These features, along with its low energy consumption, make MQTT suitable for applications such as the Internet of Things (IoT) and mobile Internet.

1. Creating the Parent Topic in Advance

Multiple levels of dynamic topics exist in the typical MQTT protocol, which eliminates the need to define and create them in advance. MQ topics are set up in advance in the MQ Console because MQTT is queue-oriented (for message persistence). MQ supports not only the MQTT protocol but also Transmission Control Protocol (TCP) and Hypertext Transfer Protocol (HTTP). To ensure intercommunication between messages of different access methods, MQTT provides the following adaptations:

  • Creates parent topic in the MQ Console before using MQ.
  • No need to create child topics.
  • Child topics used directly in code.

2. Supporting Peer-to-Peer (P2P) Without Showing Subscribers

P2P serves as a supplement to the standard MQTT protocol, which does not support P2P. All clients connected through the TCP/HTTP protocols can receive or send P2P messages.

  1. Keep topics short and concise.
  2. Use UTF-8 encoding to avoid unreadable characters.
  3. Consider embedding the unique device number into the topic in specific scenarios to facilitate identification of message senders. For example, only client1 is allowed to publish messages to client1/status and not to client2/status. When you receive messages from client1/status, you can be sure that client1 sent the message.
  4. Use the wildcard character “#” with caution. For example, if you subscribe to news/sport/#, you will receive all messages from news/sport, such as news/sport/player1, news/sport/player2, and news/sport/player1/score. This can overload the receiving terminal (mobile terminal or embedded device).
  5. Scalable — The subscription relationship should be created first so the sender and receiver can understand the existence of topics in advance. An intelligent topic structure design allows for the addition of only child topics when a new business demand emerges, without changing the parent topics or overthrowing the original topic structure.
    For example, you can design the news about Li Na in the news/lina structure. However, there could be other people named Li Na in the cultural, sports, and entertainment circles. If users want to subscribe to news about Li Na specifically in the tennis circle, the topic structure has to be changed. The suggested designs below will produce better results:

Implementation of Social IM with MQTT

Listed below are few scenarios about the implementation of social IM with MQTT:

  • Device ID: The Device ID is the unique identifier of every device. The business entity specifies the Device ID, and you do not need to create it in the MQ Console.
  • User B uses device GID_IMS@@@DeviceID_B
  • User C uses device GID_IMS@@@DeviceID_C

Scenario 1: User A requests to add User B as a friend for one-to-one chat

Solution 1: Use P2P messaging

Image for post
Image for post
Image for post
Image for post

Scenario 2: User A follows User B’s updates and receives updates when User B updates his profile

To follow User B’s updates, User A subscribes to IMS/UserB. When User B shares content, User B publishes it to IMS/UserB. Subscribers (i.e., friends) of IMS/UserB can view the post (MQTT is responsible for sending the message to all subscribers).

Image for post
Image for post
Image for post
Image for post

Scenario 3: User A invites User B and User C to join a group chat. User B and User C are not friends. Both are friends with User A.

First, the group chat initiator, User A, subscribes to IMS/Group123/#. P2P messaging is used to invite User B and User C to join the group chat.

Image for post
Image for post

Scenario 4: Send messages to a group of friends at holidays (not group chat)

You can send P2P messages to friends one by one or send messages to your friend’s inboxes. The disadvantage is you need to publish the message multiple times, depending on how many friends you want to receive the message.

Image for post
Image for post

Scenario 5: Commercial marketing — the system pushes messages to users in different categories/with different positioning

If you want to send a New Year greeting to users in various regions with the message content varying according to the region, you can use P2P or inbox, as in scenario 4. However, this is not an ideal solution for the entire system since there are many users.

Image for post
Image for post

Conclusion

MQTT is efficient in scenarios where a small code footprint is needed. It also helps you create an instant messaging app easily and quickly. MQTT is a good solution for wireless networks with flexible latency caused by bandwidth constraints.

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store