When your chat application supports more than a hundred users simultaneously it becomes almost impossible to handle all conversations in real time. To solve this problem you need an asynchronous way of sending messages directly to a user. This article presents you with the best solutions for mass messaging and explains how you can use socket.IO for that purpose. The explanation is addressed to intermediate-advanced developers with some Node.js experience, but beginners should also be able to follow along easily with some prior knowledge of Node and Express applications.
Why Do We Need Mass Messaging?
If you have ever built a real-time chat application you know that it’s hard to keep up with the load if you have more than a hundred users online at the same time. The chat will inevitably start lagging and the experience for all the users will be terrible. To avoid this, you need to introduce what is called asynchronous messaging. This means that you need to send a message to a user that doesn’t require you to wait for the response from the user to continue. There are many use cases for asynchronous messaging. You can use it for sending notifications, reminders, scheduled messages and many more.
What is Socket.IO?
Socket.IO is a client-server library that makes real-time two-way communication between web clients and servers simple. It is designed to be easy to use and have very low overhead. It is free, open-source and can be used in both client-side and server-side applications.
In short, the library acts as a bridge between a web application and a server running on a specific port. That way a client can send data to the server and receive data from the server without having to be on a particular URL.
The client sends data to the server as a message. This message is in the form of a JSON object. The server receives the message and replies with a confirmation message. The confirmation message is also JSON-shaped.
Using HapPhi for Mass Messaging
HapPhi is a hosted messaging service built on top of socket.IO. The service supports the most popular programming languages and frameworks, including Node.js and Java.
The HapPhi service works as follows:
- The user clicks a Send button, which opens a new socket connection.
- If the user is not in a group, a new group is created. If the user is in a group, the new message is sent to the group.
- The client receives the message and sends it to the server.
- The server sends the message to all the users in the group or to the user that has sent the message.
HapPhi: Another Option for Mass Messaging
HapPhi is an excellent option for implementing mass messaging. However, you don’t have to use HapPhi for that. You can also use socket.IO with a server like happhi-server. Happhi-server is a Node.js server that makes it easy to send messages from a server to all connected clients.
The following diagram illustrates how this setup works:
Limitations of the happhi-server and socket.IO combo
HapPhi has a paid tier that allows you to send more than 10,000 messages daily and gives you access to more features. HapPhi is a hosted service, so there is no way of migrating to a paid plan and hosting the functionality yourself.
The setup described above works only if you intend to send messages to all users in a group. If you want to message only one user, you have to create a new group. The problem with this is that groups slow down the application and generally aren’t a good idea for mass messaging.
If you want to implement asynchronous messaging in your application, you can use socket.IO as a library or use a service like HapPhi that supports socket.IO.
HapPhi has a free plan that gives you a certain amount of GB. It’s a hosted solution, which means that you don’t have to install and maintain any software.
Keep in mind that groups slow down the application, so you should try to avoid creating groups for every asynchronous message. Group messages together and send them as a single message.
For large-scale real-time applications, asynchronous messaging is an essential feature. If you want to send a message to more than one user, but you don’t have time to implement a custom solution, use one of the libraries or services described in this article.