How to keep a user subscribed to every chat it is following

倾然丶 夕夏残阳落幕 提交于 2019-12-04 22:23:32
Steve Pallen

In messaging apps, there are two primary concerns. Managing the list of resources (users, rooms, etc) that a user is subscribed too. This is typically displayed as a list in the client. So you need to render that list and update for thinks like presence, new message alerts, and perhaps showing if the use has an open chat window. The solution for this part is similar regardless if the users/rooms are point-to-point or multi-user (rooms).

The second concern is displaying messages in individual chat windows that are open/visible. The solution for this may vary depending on whether they are point-to-point or multi-user. However, Facebook does support multi-user chat, so I'll answer this based on my experience building a Slack clone in Phoenix.

I recommend two channels, one for users and one for the open chat windows, lets call it chat. Each user will have 1 instance of a user channel and an instance of the chat channel for each open chat window. The user_id can be used for the topic of the user channel like "user:" <> user.user_id. You will probably have a schema for each of the chat sessions (which uses are part of the chat session). So use that id for the chat channel topic like "chat:" <> chat.id.

You can then broadcast new incoming messages in the chat channel and they will go out to everyone in that chat session (people that have the chat window open). This will work for both point-to-point and multi-user windows.

The example you provided in the question shows two uses with different state (text color). One shows a missed call and the other I presume indicates an unread message. This type of state is typically specific to the user, so you would use the use channel put push this information.

To track indicators on the user list, you can subscribe to messages in the chat channel from the user channel. You will then receive info messages in the user channel and take appropriate actions if needed. If you subscribe to the chat join messages, you can build a list of these and store it in your socket.assigns struct. See Phoenix Channels - Multiple channels per socket for more details.

Another API that I use for messaging apps is the intercept api. This allows you define a handle_out function that gets called for specific broadcast events of a channel. It can be used to filter or manipulate outgoing messages broadcast on that channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!