SAMA: chat server tech stack

Igor Khomenko
SAMA communications
5 min readJun 7, 2023
Tech stack logo

In the world of real-time communication, chat servers play a vital role in connecting people across various platforms. To build a robust and scalable chat server, selecting the right technology stack is crucial.

In this article, we will delve into SAMA tech stack that combines Node.js, MongoDB, Redis and S3. This combination empowers developers to create feature-rich chat applications capable of handling high traffic loads and ensuring seamless user experiences.

Core — uWebSockets/Node.js

As for the core, we use the powerful combination of Node.js and uWebSockets, a high-performance WebSocket framework. By leveraging uWebSockets, developers can create chat servers capable of handling massive concurrent connections, facilitating real-time communication between clients and servers.

uWebSockets is a lightweight and efficient WebSocket framework designed specifically for Node.js. It harnesses the power of the libuv library, which appears to be the same underlying library that drives Node.js’s event-driven I/O operations. The framework is known for its exceptional performance, scalability, and resource efficiency, providing an excellent choice for building high-performance chat servers.

Also, uWebSockets boasts a small memory footprint and low CPU usage, making it an ideal choice for resource-constrained environments. This efficiency allows chat servers to handle a large number of concurrent connections without overwhelming system resources.

Data Storage — MongoDB

The way of selecting the right data base is crucial to ensure the scalability, performance, and flexibility of chat servers. At SAMA we use MongoDB — a NoSQL document database — as for the underlying data base. By leveraging MongoDB’s unique features and capabilities, developers can build robust and scalable chat applications capable of handling high volumes of data with ease.

MongoDB is a highly popular NoSQL document database that stores data in flexible, JSON-like documents. It offers an extensible and and highly effective solution for managing chat-related data such as messages, user profiles, chatroom information, and more. MongoDB’s schema-less nature allows for agile development, enabling developers to easily adapt and evolve the data model as the chat application evolves.

MongoDB is designed to scale horizontally by distributing data across multiple servers or clusters. With sharding and replica sets, developers can handle high traffic loads and accommodate growing user bases effortlessly. As the chat application’s user base expands, MongoDB’s scaling capabilities ensure that performance remains optimal.

Files Storage — S3

In chat applications, file sharing is a common requirement. AWS S3 (Simple Storage Service) offers a reliable, scalable, and cost-effective solution for storing and retrieving files. Integrating AWS S3 into the chat server tech stack provides several benefits:

  • a. Scalability: AWS S3 is designed to handle massive amounts of data and high volumes of concurrent requests. It provides automatic scaling, ensuring that your chat server can accommodate growing file storage requirements without sacrificing performance.
  • b. Durability and Availability: AWS S3 offers exceptional durability, with data stored redundantly across multiple facilities and devices. This ensures that files are protected against hardware failures, providing high availability for chat server users.
  • c. Security: AWS S3 provides robust security features, including encryption at rest and in transit. It also supports fine-grained access control through AWS Identity and Access Management (IAM) and bucket policies, allowing you to control those who can access and manipulate files in the storage.

As for the files storage, SAMA uses S3 based storage, which allows you to use any storage system from the list of S3, Minio and Spaces. All 3 storages are supported in SAMA, allowing to use Minio mainly for local development, Spaces while deploying on Digital Ocean and S3 for when deploying on AWS.

Data Cache — Redis

To ensure optimal performance and scalability, it is crucial to have data cache implemented and use it for ‘hot’ data access while reducing interruction with data base layer. At SAMA we use Redis — an in-memory data cache — as for the underlying data cache layer. By leveraging Redis’s caching capabilities, developers can build high-performance chat applications capable of handling large volumes of data and providing real-time responsiveness.

Redis is an open-source, in-memory data structure store that serves as a cache, message broker, and real-time database. It excels in use cases that require fast data access, low latency, and high throughput. By storing frequently accessed chat-related data in Redis, we can dramatically improve the performance and responsiveness of SAMA servers.

Redis’s in-memory nature allows for extremely fast data access, delivering sub-millisecond response times. This enables chat servers to handle high volumes of concurrent requests and provide real-time responsiveness, enhancing the user experience and reducing message delivery delays.

Redis’ architecture allows for horizontal scaling through replication and sharding. By adding more Redis nodes and distributing the data across multiple instances, chat servers can handle increasing loads and accommodate growing user bases seamlessly.

By identifying the chat-related data that is frequently accessed and would benefit from caching, using Redis can improve the data access time. This may include user profiles, online status, chatroom metadata, and recent message history. Utilize Redis’ caching capabilities to store and retrieve this data, reducing the load on the primary data storage system (e.g., MongoDB) and improving chat server performance.

Message Queue — Bull

Push notifications play a crucial role in keeping users engaged and informed in chat applications. To build efficient and scalable chat servers capable of delivering real-time push notifications, selecting the right technology stack is essential. We explore the integration of Bull, a powerful message queue library, into the chat server tech stack specifically for handling push notifications. By leveraging Bull’s message queuing capabilities, we can optimize the delivery of push notifications, ensuring timely and reliable communication with chat application users.

Bull is a Redis-based message queue library that facilitates asynchronous processing of tasks and distributed job management. It provides reliable message queuing, making it an excellent addition to the chat server tech stack for handling push notifications. With Bull, we can efficiently manage push notification delivery, prioritize notifications, and ensure fault-tolerant message processing.

Bull’s distributed message queuing capabilities allow chat servers to scale horizontally. Multiple worker processes or servers can process push notification jobs concurrently, ensuring efficient load balancing and handling increasing notification volumes as the user base grows.

Bull ensures reliable push notification delivery even in the event of server failures or restarts. Messages remain in the queue, preventing data loss and guaranteeing that notifications are processed once the server is back online, ensuring a seamless user experience.

Conclusion

The combination of Node.js, MongoDB, Redis, and S3 provides a robust and scalable tech stack for building feature-rich chat servers. Leveraging the asynchronous capabilities of Node.js, developers can create real-time communication channels. MongoDB offers flexibility in data storage, while Redis ensures high-performance message delivery and event-based communication. S3 handles scalable object storage for media assets. By utilizing this powerful tech stack, we can build chat applications that are capable of handling high traffic loads, provide rich and flexible API while providing optimal performance and high availability.

Try SAMA

To run SAMA server locally — follow https://github.com/SAMA-Communications/sama-server. Frontend app is available at https://github.com/SAMA-Communications/sama-client

Discover, enjoy, and all feedback is welcome. We will be thankful a lot for every GitHub star, issue or comment!

--

--

Igor Khomenko
SAMA communications

Software craftsman, RTC pro, love programming and clouds, XMPP, WebRTC, secure messaging, chat bots