11

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?

user3746259
  • 1,491
  • 2
  • 23
  • 46
  • No ideas? I think this is something everyone should have thought about when it comes to websockets, so there must be a recommended way to do that. – user3746259 Oct 02 '15 at 14:03
  • Would you really want all 1m users to chat in the same group? Otherwise, you obviously need to partition things into sub-groups. Same with notifications. In addition to the performance implications, not all users should receive all notifications for privacy reasons. – gzost Oct 02 '15 at 15:11
  • I didn't mean chatting or sending notifications in one big group - I meant in one big channel and adress them to the specific users. – user3746259 Oct 02 '15 at 15:56
  • Why have one big channel (presumably with the recipient as part of the payload) instead of having dedicated channels for user groups, or per-user topics if you want to address single users? – gzost Oct 02 '15 at 16:07
  • @user3746259, in this case, you're misusing the jargon "channel." From the context, it seems that you're thinking of a socket, the logical representation of the way users connect to your server. The word channel means a broadcast domain: someone sends a message to the channel and everyone subscribed to that channel receives it. -- So, do you mean a channel or a connection socket? (Or, do you mean something else entirely, and need help coming up with the correct term for it?) – Ghedipunk Oct 02 '15 at 16:22
  • I meant Channels in sense of Topics. I think I understand now: For notifications, I use per-user topics (by identitfying with a passed user_id) - and for chats, I create "private chat rooms" where two or more users can connect to (by passing multiple user ids as parameters to the topic) ? – user3746259 Oct 02 '15 at 16:46
  • And when it comes to "chat topics", I have the opportunity to send from a client directly in javascript a message to this topic or from the server. But when it comes to a per-user notification topic, only the server can send messages to this topic.. right ? – user3746259 Oct 02 '15 at 16:52

1 Answers1

3

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.

Quentin Skousen
  • 1,035
  • 1
  • 18
  • 30
  • Nice approach, but as i read above, I came to the conclusion to create per user channels for notifications and private chat channels where specific user Groups can chat in. That is, I think, a better seperated model. – user3746259 Oct 02 '15 at 18:55
  • * this differs from yours because you would only have one channel per user and no seperated chat channels. The security Checks can also be done on Server side in the Moment the user tries to subscribe to one channel – user3746259 Oct 02 '15 at 18:56
  • ** and you could not use the approach to send messages directly from Client to client just by using js - but I think this is not so Bad because Server side Checks Must be done in all cases always – user3746259 Oct 02 '15 at 19:06
  • I guess I am confused as to what you are trying to do. Sorry – Quentin Skousen Oct 02 '15 at 22:50
  • Using websockets for notifications and chats the right way. – user3746259 Oct 02 '15 at 23:03