Real-Time Communication (WebSockets)
Relevant source files
The following files were used as context for generating this wiki page:The Ever Demand platform is designed to be reactive and real-time, providing instant updates across all its applications. This is achieved through a robust WebSocket-based communication system that supports both custom real-time API endpoints and GraphQL subscriptions. The backend leverages the Socket.IO library, integrated into the NestJS framework, and utilizes a custom-built micro-framework called Pyro IO to simplify exposing services over WebSockets.
This system is fundamental for features like real-time order management, delivery tracking, and live updates for promotions and product availability. Client applications, particularly those built with Angular, use a dedicated SocketService to manage the connection and interact with the server's real-time APIs.
Sources: README.md, packages/core/README.md
Architecture Overview
The real-time architecture consists of a Socket.IO server running within the core NestJS backend and various clients (web and mobile apps) connecting to it. The server exposes two primary types of real-time communication:
- Pyro IO API: A custom RPC-style API over WebSockets that allows clients to call decorated service methods directly.
- GraphQL Subscriptions: Standard Apollo GraphQL subscriptions for event-based data streams.
The diagram below illustrates the high-level communication flow between a client application and the backend server.
This diagram shows how client applications establish a WebSocket connection to the main Socket.IO server, which then delegates requests to either the Pyro IO handler for custom API calls or the GraphQL server for subscriptions.
Sources: packages/core/README.md, packages/core/src/app.module.ts, packages/common-angular/src/lib/socket.service.ts
Server-Side Implementation
The server-side WebSocket functionality is primarily managed within the @ever-platform/core package. It combines NestJS modules, the Socket.IO library, and the custom Pyro framework.
Pyro IO Micro-Framework
Pyro IO is a custom micro-framework designed to easily expose service methods via WebSockets without needing to write separate controllers. It uses decorators to mark classes and methods that should be accessible through the Socket.IO API.
Sources: packages/core/README.md
Key Decorators
| Decorator | Description |
|---|---|
@routerName(name) | Marks a service class as a router, making its methods available over WebSockets under the given name. |
@observableListener | Decorates a method that returns an RxJS Observable. This is used for streaming real-time data to the client. |
@asyncListener | Decorates a method that returns a Promise. This is used for one-time request/response interactions with the client. |
Sources: packages/core/README.md, packages/core/src/@pyro/io/connection-handler.ts:11-12
Example Usage
// Example from packages/core/README.md
@routerName('users')
class UsersService {
// returns Observable!
@observableListener()
get(id: string): Observable<User> {
// ...
}
// returns Promise!
@asyncListener()
async register(user: IUserCreateObject): Promise<User> {
// ...
}
}
Sources: packages/core/README.md
Connection Handling
The ConnectionHandler class is the heart of the Pyro IO framework. When a new client connects to the Socket.IO server, this handler is responsible for discovering all decorated services (@routerName) and dynamically binding their decorated methods (@observableListener, @asyncListener) to corresponding socket events.
The flow for handling a new connection is as follows:
This diagram shows the process from a client connecting to the server to the point where its methods are callable via socket events.
The _getRouters method scans the dependency injection container for services that have the @routerName decorator and prepares them to be bound to socket events.
Sources: packages/core/src/@pyro/io/connection-handler.ts:31-74
GraphQL Subscriptions
Alongside the custom Pyro IO API, the platform uses standard GraphQL subscriptions for event-based communication. The SubscriptionsService is responsible for creating and managing the subscription server.
Key aspects of the implementation:
- It uses the
subscriptions-transport-wslibrary to create aSubscriptionServer. - The server is installed on the main HTTP server instance.
- The default endpoint for WebSocket connections for subscriptions is
/subscriptions. - The
onConnecthandler within the service is used to authenticate the connection, typically by verifying a JWT token passed in the connection parameters.
This is enabled in the main application module:
// packages/core/src/app.module.ts
@Module({
//...
imports: [
//...
SubscriptionsModule.forRoot(env.GQLPORT_SUBSCRIPTIONS),
GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
installSubscriptionHandlers: true,
//...
}),
//...
]
//...
})
export class ApplicationModule {
//...
}
Sources: packages/core/src/graphql/subscriptions/subscriptions.service.ts:24-57, packages/core/src/app.module.ts:182-188
Client-Side Implementation
On the client side, the @ever-platform/common-angular package provides a SocketService to abstract away the complexities of WebSocket communication.
SocketService
This Angular service acts as a singleton wrapper around the socket.io-client. It manages the connection lifecycle, authentication, and provides simple methods for sending and receiving events.
The class diagram for SocketService is shown below.
This diagram outlines the main properties and methods of the SocketService.
Sources: packages/common-angular/src/lib/socket.service.ts
Connection and Authentication Flow
When a client application needs to establish a real-time connection, it calls the connect method on the SocketService, usually providing a JWT for authentication.
The sequence of events is as follows:
This sequence diagram illustrates the handshake process, including the authenticate event sent from the client to the server upon connection.
Sources: packages/common-angular/src/lib/socket.service.ts:39-75
Key Methods
| Method | Description |
|---|---|
connect(token?: string, options?: any) | Initializes the connection to the WebSocket server. If a token is provided, it attempts to authenticate. |
disconnect() | Disconnects the socket from the server. |
emit(event: string, ...args: any[]) | Emits an event with data to the server. This is used to call methods on the server's Pyro routers. |
on(event: string) | Returns an Observable that listens for a specific event from the server, allowing the client to react. |
Sources: packages/common-angular/src/lib/socket.service.ts:39-100
Summary
The real-time communication system in Ever Demand is a cornerstone of its reactive architecture. By combining the industry-standard Socket.IO and GraphQL subscriptions with the custom-built Pyro IO micro-framework, the platform provides a powerful and developer-friendly way to build real-time features. The server-side ConnectionHandler dynamically exposes service logic, while the client-side SocketService provides a clean and simple API for Angular applications to consume real-time data, enabling a seamless and interactive user experience.
Sources: README.md, packages/core/README.md, packages/core/src/@pyro/io/connection-handler.ts