Skip to main content

Messages Module Documentation - EML Backend

1. Introduction

The Messages module in EML Backend manages the sending and receiving of messages on the platform. It integrates instant messaging functionality and communication with clients through different channels.


2. Code Structure

modules/messages/
├── messages.module.ts # Module definition
├── messages.controller.ts # HTTP Controller
├── messages.service.ts # Business logic
├── messages.gateway.ts # WebSockets for real-time communication
├── messages.repository.ts # Database access
├── dto/ # Data Transfer Objects (DTOs)
│ ├── send-message.dto.ts
│ ├── receive-message.dto.ts
├── entities/ # Database models
│ ├── message.entity.ts

3. Available Endpoints

3.1 Send Message

POST /messages/send - Sends a message to a user or channel.

Request Example:

curl -X POST http://localhost:3000/messages/send      -H "Content-Type: application/json"      -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c..."      -d '{"recipient": "+50312345678", "message": "Hello, this is a test message."}'

Response Example:

{
"id": 101,
"recipient": "+50312345678",
"message": "Hello, this is a test message.",
"status": "sent"
}

3.2 Get Message History

GET /messages/history - Retrieves the history of sent messages.

Request Example:

curl -X GET http://localhost:3000/messages/history      -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c..."

Response Example:

[
{
"id": 100,
"recipient": "[email protected]",
"message": "Test message",
"status": "delivered",
"timestamp": "2025-03-03T12:34:56Z"
},
{
"id": 101,
"recipient": "[email protected]",
"message": "Hello, how are you?",
"status": "read",
"timestamp": "2025-03-03T12:35:10Z"
}
]

3.3 WebSockets: Real-Time Messaging

The module allows real-time communication using WebSockets.

Example WebSockets Connection:

const socket = io('http://localhost:3000');
socket.on('connect', () => {
console.log('Connected to the messaging server');
});
socket.emit('sendMessage', { recipient: 'user1', message: 'Hello!' });
socket.on('newMessage', (data) => {
console.log('New message received:', data);
});

4. Security and Middleware

  • JWT Authentication: All requests require a valid JWT token.
  • Message Filtering: Moderation rules are applied to prevent inappropriate content.
  • Rate Limiting: Message sending frequency is limited to prevent spam.

Example of middleware for protection:

@UseGuards(AuthGuard('jwt'))
@Post('send')
async sendMessage(@Body() messageDto: SendMessageDto) {
return this.messageService.processMessage(messageDto);
}

5. Dependencies with Other Modules

  • Users: To identify senders and recipients.
  • Notifications: To send alerts for critical messages.
  • Reports: To generate statistics on messaging usage.

6. Additional Notes

  • Plans to implement support for multimedia messaging (images, videos).
  • Considering integration with SMS and WhatsApp providers for external messages.

7. Conclusion

The Messages module in EML Backend enables efficient communication within the platform, supporting both instant and historical messages.

🚀 This document will be updated as the system evolves.