Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 390
user3746259 Avatar asked Oct 01 '15 14:10

user3746259


People also ask

Is WebSocket good for chat app?

The answer is also: definitively yes. The proof are benchmarks. A single server can handle easily more than 1000 websocket connections without problem.

What is WebSocket chat?

WebSockets enable seamless two-way communication between a client and server. In other words they allow websites to send and receive data in real time. Developers can use WebSockets to build chat applications, multiplayer games, or user interfaces which expose events in real-time, and much more.

What are WebSocket notifications?

WebSocket Notifications The main aim of WebSockets is to provide real-time notifications for clients about server events or changes without a need for the client to repeat requests to the server for new information.

Does Telegram uses WebSockets?

Messaging protocols like WebSockets and XMPP are two of the most widely used instant messaging protocols in the market - being used by messaging apps like Whatsapp, Telegram, and Hike.


1 Answers

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.

like image 127
Quentin Skousen Avatar answered Nov 06 '22 02:11

Quentin Skousen