user3746259
user3746259

Reputation: 1581

Websockets Notification- / Chat- System

I've read a lot about websockets and already implemented them within my system. This question is about how to use them properly. I want to implement a notification and a chat system the right way.

For notifications, I have the channel "notifications/channel" and for chats, I have the channel "chats/channel".

Aren't these two channels too "global" ? Let's say when the site has 1.000.000 users, this would mean all these users would be in these two channels. When one notification gets sent to another specific user, this would mean, that the message gets sent through a channel, where 1.000.000 users subscribed to.

Same with chat messages. Let's say a user wants to chat with another user. Each message would pass the channel where all users subscribed to and in the end, only the target user would receive the message due to a passed receiver_id.

How to handle properly notification channels and "private" chat channels?

Would it be more performant and secure to create for each User a "sub channel " (Group chats And notifications, E.G "notifications/channel/user1"), or just let all users in one big channel?

Upvotes: 11

Views: 2129

Answers (1)

Quentin Skousen
Quentin Skousen

Reputation: 1055

Personally, the way I would address this is as follows:

Each user has 1 websocket connection. This connection would be used to pass all data. I would use a json format to pass data back and forth. I would use a field in the json struct to indicate the type of message, and other info such as a chat room ID. So, if I wanted to send a notification, it could be something like this (really simple example):

{
    "type":"notification",
    "message":"New Mail"
}

A chat message would be something like this:

{
    "type":"chat",
    "chatID":4756,
    "message":"Hello, world!"
}

Client-side Javascript logic would determine the type of message and what to do with it. Server-side logic would determine if the user is "subscribed" to the specified chat room, so it would know what chat messages to send to what user. This would keep it secure, so you aren't sending chat messages to users who are not subscribed to a room ID.

Let me know if you need any clarification on this method.

Upvotes: 3

Related Questions