You can use publish-subscribe pattern. Let’s understand what is publish-subscribe pattern.
publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead characterize published messages into classes without knowledge of which subscribers, if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.
Source: Wikipedia
Below is an example using RabbitMQ MQTT Adapter:
Subscribe user A’s app to a topic “/topic/user-a”, user B’s app to a topic “/topic/user-b” and publish the online/offline status to a topic "/topic/presence". Create a program on your backend server to subscribe to the "/topic/presence". If any update comes from lets say User A then publish the update to all friends to User A. This way, User B will receive the online/offline update of User A.
User A User B PresenceListener
Subscribe /topic/user-a /topic/presence /topic/presence
Publish /topic/user-b /topic/presence friend list
Real challenge here is how to publish "offline". One case is if user closes the app while internet is still active then the app can publish the "offline" status to the server but what happens when the internet stops working?
Lets go through "last will and testament" (lwt).
LWT messages are not really concerned about detecting whether a client has gone offline or not (that task is handled by keepAlive messages). LWT messages are about what happens after the client has gone offline.
LWT messages can be leveraged to define a message to be published by the broker on behalf of the client, since the client is offline and cannot publish anymore.
Source: http://tuanpm.net/what-is-mqtt/
For a sample source code with online offline presence, you can checkout our Applozic Chat Javascript Plugin code available in Github https://github.com/Applozic/Applozic-Web-Plugin/
Demo page: https://www.applozic.com/docs/chat-examples/web.html