Featured Image
Software Development

Build Your Own Personal and Group Chatting Workflow using PubNub

In a previous blog, we introduced the basic workings of PubNub. If you are not familiar with PubNub, I have covered it in a previous blog of this series. You can go through it here. In this blog, we will see how to utilize PubNub concepts to design the chat workflow in real use cases. 

In this blog, we will discuss the use case of implementing personal/group chat. Many products provide functionality to chat personally or in groups. For example, consider that you are running a coaching academy and you have an application to manage it. You would definitely like to provide functionality for students and faculty to chat in person or groups. So, how can PubNub be useful for implementing and integrating 1:1 and group chatting? Let’s see it in detail!

Here, we will focus on implementing a chatting mechanism without any extra add-ons like file/image sharing, push notifications, etc. We will also cover these scenarios in our upcoming blogs in this series!

Note that in this blog, we are not discussing the implementation in any specific programming language or framework. Our focus is mostly on designing end-to-end workflows for such a use case and how PubNub is useful for that.

Prerequisites 

Before proceeding further in this article, there are certain prerequisites you need to take care of : 

  • You must have a basic idea of PubNub, how it works and its core concepts. Basic programming knowledge and ideas about the client-server paradigm. (It is not compulsory to have an idea only in a specific programming language)

Scenario : 

Alright! Let’s see what we are going to design! 

Assume that you have a successful product and have an application ready for it. The product can be anything: A mobile application or web application for a coaching institute, for gym members, etc.

In that application, we need to implement and integrate a chat system which allows users to have an account and allows them to communicate in personal as well as in groups. 

Here, we assume that we have an API running on a backend server which will take requests to create personal chat rooms and groups. The backend server will generate the channel names according to a decided and consistent naming convention. 

Additionally, the APIs will provide the list of conversation channels (personal and group both) to which the user is involved in. The frontend will subscribe to those channels and publish messages to relevant channels when the user wants to send something.

Now that we are clear about what we are designing, let’s jump straight into it. Let’s address the following important questions about designing workflow for our use-case.

Who Will Be Involved in Message Sending/Receiving?  

The first important thing while designing use cases with PubNub is deciding who will be involved in communications. In our use case, different users who have an account on your app will be involved in communicating/sharing messages.

Which Different Types Of Channels We Need To Define?

The next thing to consider is what type of channels we will consider in our workflow. As discussed in a previous blog in the series, the channel is a medium where messages will be published. In our use case, we need 2 types of major communication channels: personal chats and group chats. We will need to have dedicated PubNub channels for each group and personal chat rooms. Additionally, we will also need one channel specific to every user in the app. We will discuss its purpose in upcoming sections.

What Will Be the Naming Conventions Of Channels?

Once we are done with deciding on channels, we will need to define clear and consistent naming conventions for all types of channels. It is important to have consistent naming for channels to improve the maintainability and extensibility of our workflow. We will discuss this in detail in the upcoming section.

Now, we are clear about users, channels and their naming conventions for our use cases. We will focus on a complete detailed workflow step by step. 

Detailed End-To-End Workflow Discussion

  1. Decide which information to store on the backend
  • User details along with PubNub userId.
  • 1:1 chatroom data – Includes details about chatroom members involved and generated the channel name for the same.
  • Groups data – Includes details like group name, profile pic, and generated PubNub channel name for the group.
  • Necessary APIs to fetch the above data when required.

  1. Decide types and consistent naming conventions for PubNub  channels for your application
  • For chats: personal.<userId1>_<userId2> (user1Id will always be less than user2Id) Ex. : If a chatroom is to be created between users with IDs 4 and 3, the channel will be personal.3_4. Users who want to chat in private can create a chatroom and subscribe to the corresponding 1:1 channel to send/receive private messages.
  • For groups: groups.<groupId>. Multiple users who are part of the same group can subscribe to the corresponding group channel to send and receive group messages.
  • Personal channel or user channel: user_<userId>. The purpose of this channel is to receive some operational updates as a message in real-time. For example, if user A creates a chat with user B, and if user B is on the chat conversation screen, that screen should be updated in real-time to show the conversation with user A. 

  1. Flow for 1:1 chats: 
  • Make an API request to create a 1:1 chat. Send user IDs of the involved user as a request payload.
  • The backend performs the validations and generates the PubNub channel name from the data it receives and sends it as a response.
  • On receiving a success response from the server, send an operational message to the receiver’s personal channel to notify them in real time. 
  • Mobile devices can use the channel name to subscribe and send/receive messages for that specific 1:1 chatroom.

  1. Flow for group chats: 
  • Make API requests to create a group chat. Send user IDs of the involved user as the request payload
  • The backend stores necessary details about groups and their members in the database.
  • The backend generates the PubNub channel name for the group from the data it receives and sends it as a response.
  • On receiving the success response from the server, one operational message will be sent to the personal channels of all users to notify them in real-time and take action if they are on the chat conversation screen.
  • Mobile devices can use that channel to subscribe and send/receive messages from that.

  1. API to fetch conversation data from the backend: 
  • We need an API which gives data of conversations in which any user is involved. This will include details of all 1:1 chats/groups with which the user is associated.

  1. Overall workflow :
  • When a user logs in to the system, fetch the list of 1:1 chats and groups in which he/she is involved from the backend using the API mentioned above.
  • Start listening to the personal channel for users.
  • Based on that data, subscribe to those channels and display the UI according to conversation data, setting appropriate listeners for those channels.

Advantages of the basic approach

  • Simple
  • Minimal backend server involvement – only to keep track of the groups and 1:1 conversations that the user is a member of.

Disadvantages of the basic approach

  • Heavy dependency on mobile or client side for lots of operations: subscribe, unsubscribe etc. 
  • Increased chance of failing operations because of network issues
  • Tough to handle certain scenarios related to the initialization of groups or personal chats due to client-side dependencies.

Conclusion

In this blog, we have seen how to use PubNub to implement and integrate personal and group chatting functionality in our applications. We have discussed the use case, the prerequisites, the scenario, the workflow, and the advantages and disadvantages of our approach. 

PubNub is a powerful and reliable platform that provides real-time communication and scalability for chat applications. We hope you have enjoyed this blog and learned something new. Stay tuned for more blogs in this series where we will explore more features and use cases of PubNub. Thank you for reading! 

Are you ready to build your own personal and group chatting workflow using PubNub? If you need any expert assistance, we are here to help. As a certified PubNub advanced tier integration partner, we have the skills and experience to make your chat project a success. Get in touch with us today and discover how we can transform your ideas into reality.

We also offer top-notch software product development services. Whether you need a web app, a mobile app, or a cross-platform app, we can deliver high-quality solutions that meet your needs and expectations. Check out our portfolio to see some of the amazing projects we have done for our clients.

If you enjoyed this blog and want to learn more about chat applications, you might also like these blogs:

The Ultimate Tutorial for Building Flutter Chat Apps with PubNub

How to Create a Flutter Group Chat App with Role-Based Access using CometChat

How to Build a Flutter Chat App with 1-on-1 Messaging using CometChat

author
Soham Patel
I am a software developer with keen interest in back-end development and DevOps. I am passionate about and have experience in understanding business requirements and translating those into efficient, well-maintainable and reliable software products.