Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ws): simplify events for web sockets queue for performance #4152

Conversation

p-fernandez
Copy link
Contributor

What change does this PR introduce?

From the Worker we will only send one event instead of 3 and we will process it executing the functions that before were triggered by 3 different events, as they are sent at the same time.
We also only process the event and trigger the messages to the WebSocket if the user is online as if not it is a waste of resources.

Why was this change needed?

It is a performance improvement identified during the MemoryDB migration.

Other information (Screenshots)

@p-fernandez p-fernandez self-assigned this Sep 12, 2023
@linear
Copy link

linear bot commented Sep 12, 2023

NV-2824 WebSockets: Simplify the job events created when sending an InApp notification

What?

Send one single event adding one single job to the WebSockets queue instead of 3 as we do right now.

Swift the logic around in ExternalServicesRoute:

public async execute(command: ExternalServicesRouteCommand) {

to check if the user is online and if not not to emit any of the current 3 events for an InApp notification sent. If user is online we can send the 3 events: Unseen, Unread and Received.

Why? (Context)

Right now we are sending 3 events when sending an InApp message:

await this.webSocketsQueueService.bullMqService.add(

This seems non performant as it triples the usage of the resources for the ws_socket_queue queue (JobTopicNameEnum.WEB_SOCKETS).

Definition of Done

  • Adding tests to verify the new behaviour.
  • InApp notifications kept being sent.
  • WebSocket messages for the 3 events are kept sent when a user is online.
  • WebSocket messages are not sent when user is not online.
  • User keep seeing the events when the Notification Center queries the notifications.

@@ -247,7 +247,7 @@ export class SendMessageInApp extends SendMessageBase {
);

await this.webSocketsQueueService.bullMqService.add(
`sendMessage-received-${message._id}`,
message._id,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will use the messageId as the jobId now we will have an unique event sent, therefore one unique job created.


constructor(private wsGateway: WSGateway, private messageRepository: MessageRepository) {
this.bullMqService = new BullMqService();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't used. The dependency with BullMQ is attached in the WebSocketWorker in this same app.

Comment on lines +23 to +28
if (command.event === WebSocketEventEnum.UNSEEN) {
await this.sendUnseenCountChange(command);
}

return;
if (command.event === WebSocketEventEnum.UNREAD) {
await this.sendUnreadCountChange(command);
Copy link
Contributor Author

@p-fernandez p-fernandez Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Headless sends this events to request the count changes, so keeping them.
Though would appreciate a second opinion if they are needed and we should keep it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The headless and notification-center actually are listening for these events. The events are triggered when the messages are marked as read/seen through the API or the notification-center.

Copy link
Contributor

@LetItRock LetItRock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an awesome improvement, thanks! 🙌

Comment on lines +23 to +28
if (command.event === WebSocketEventEnum.UNSEEN) {
await this.sendUnseenCountChange(command);
}

return;
if (command.event === WebSocketEventEnum.UNREAD) {
await this.sendUnreadCountChange(command);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The headless and notification-center actually are listening for these events. The events are triggered when the messages are marked as read/seen through the API or the notification-center.

Base automatically changed from nv-2823-ws-reduce-the-payload-size-of-the-jobs to feat-ws-performance-improvements September 13, 2023 11:58
@p-fernandez p-fernandez merged commit 31eb912 into feat-ws-performance-improvements Sep 13, 2023
@p-fernandez p-fernandez deleted the nv-2824-websockets-simplify-the-job-events-created-when-sending-an branch September 13, 2023 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants