This blog will focus on the architecture, components, and flow of the application, and by the end, you'll have a clear understanding of how real-time translation works in a distributed system.
System Architecture Overview
The architecture revolves around WebSocket communication for real-time message exchange, Redis Pub/Sub for handling translation tasks asynchronously, and the Google Translate API to power the translation engine. Below is a detailed overview of the components and their interactions.
Architecture Diagram

Key Components
- Client Browser: The client connects to the server via WebSocket, sending and receiving messages in real-time.
- NestJS WebSocket Gateway: The server-side gateway that handles WebSocket connections from clients, allowing real-time message exchange.
- Message Handler: Processes incoming messages, stores them in the database, and triggers the translation pipeline.
- Redis Pub/Sub: Redis acts as a message broker, managing the distribution of messages for translation across multiple translation workers.
- Translation Worker: A worker service that subscribes to Redis channels, receiving messages that need translation. It interacts with the Google Translate API to fetch translations.
- Google Translate API: Provides real-time language translation for the messages.
- PostgreSQL: The database where original and translated messages are stored.
Sequence Flow
To get a better sense of how the components work together, let's walk through the flow of translating a message in real time.
Sequence Diagram
Detailed Breakdown of Each Step
-
Client Sends a Message: The user sends a message via the WebSocket connection to the server.
-
Message Processing by WebSocket Gateway: The NestJS WebSocket Gateway receives the message and passes it to the message handler.
-
Message Handler Stores the Original Message: The message handler stores the original message in PostgreSQL for persistence and future reference.
-
Publishing the Message for Translation: After storing the message, the message handler publishes it to a Redis Pub/Sub channel to be picked up by translation workers.
-
Translation Worker Picks Up the Task: The worker listens to the Redis channel and picks up the message for translation. It sends a request to the Google Translate API to translate the message into multiple target languages.
-
Translations Returned to the Worker: The Google Translate API returns the translations, which are then published back to Redis by the worker.
-
Receiving and Storing Translations: The message handler listens for the translated messages from Redis, stores the translations in PostgreSQL, and sends the translated messages back to the client via the WebSocket connection.
-
Client Displays Translations: Finally, the translated messages are received by the client and displayed in real time to the user.
Why This Architecture?
-
Scalability: By using Redis Pub/Sub, this architecture can scale horizontally. You can add more translation workers to handle increased load without modifying the core application logic.
-
Real-time Communication: WebSockets provide low-latency, real-time communication, making it ideal for chat applications. Users can instantly see their translated messages.
-
Fault Tolerance: Redis Pub/Sub ensures that if a worker fails, another worker can pick up the task. Additionally, all messages and translations are stored in PostgreSQL, so no data is lost.
-
Separation of Concerns: Each part of the system has a specific responsibility, from message handling and storage to translation and client communication. This modularity makes the system easier to maintain and extend.
Technologies Used
-
NestJS: A progressive Node.js framework that allows us to build efficient, scalable server-side applications. The WebSocket Gateway is handled by NestJS, making it easy to manage real-time communication.
-
Redis Pub/Sub: Redis is used as a message broker, which allows asynchronous and distributed communication between different parts of the system.
-
Google Translate API: This API powers the core translation functionality, providing translations for multiple languages.
-
PostgreSQL: A reliable relational database used to store both the original and translated messages, ensuring that the chat history is persisted.
Conclusion
This architecture provides a robust, scalable solution for real-time translation in a chat application. By leveraging NestJS for WebSocket management, Redis Pub/Sub for distributed message handling, and the Google Translate API for translation services, you can deliver a seamless multilingual chat experience to your users.
Building such a system involves understanding how to integrate multiple services and how to handle messages efficiently across different components. This architecture serves as a foundation for more advanced use cases, such as translation caching or supporting additional translation services.
If yo're looking to implement real-time translation in your applications, I hope this guide provides you with the insights you need to get started!
Interested in more projects like this? Check out my portfolio for more insights into my work on real-time applications, distributed systems, and cloud architecture.


