Fix for Issue https://github.com/djangoflow/django-df-chat/issues/2
The current implementation of RoomConsumer allows user to connect to a single room via websocket. In order to connect to a different room, the user has to create a new websocket connection. Maintaining multiple connections is expensive. The server may exhaust the connection pool. So, there is a need to use a single connection and communicate across multiple rooms.
Also, the "is_online" field was placed on the RoomUser. This means that only the users connected to that particular room will know if a user is online. But, we should rather notify all the rooms a user is part of that the user is online.
New Implementation:
Moved the 'is_online' field from the RoomUser model to a new model named "UserChat".
To avoid disrupting users who are currently online, the migration process first creates the UserChat model and then creates UserChat objects for users who are currently online, setting their 'is_online' attribute to True. Finally, we delete the 'is_online' field from the RoomUser model.
Also, its worth noting that the 'UserChat' model can hold attributes and fields that are common to all rooms.
Added UserChatAdmin. Now, the "is_online" column can be found in the UserChat listing page instead of RoomUser.
Renamed RoomConsumer to RoomsConsumer, as it now allows users to connect to multiple rooms.
Removed the "room_id" from the url path for consumer. The new endpoint is "/ws/chat/".
Created a method named "subscribe_to_rooms_activities", which is used to subscribe to all the rooms. This is called when the connection is established.
Also, the RoomsConsumer.unsubscribe_from_all_activities method can be used to unsubscribe the user from listening to any events occurring on the websocket. This is called when the connection is closed - disconnected.
Usage:
The consuming application should change the websocket connection url to "/ws/chat". As we now send events from multiple rooms while being connected to a single websocket, you should segregate the events by room id, before displaying them in the UI.
Refactoring not related to this PR's Intent
We don't have to declare a separate method on the consumer for defining the serializer for an activity. We can just set the "serializer_class" argument in the model_observer decorator.
Intent:
Fix for Issue https://github.com/djangoflow/django-df-chat/issues/2 The current implementation of RoomConsumer allows user to connect to a single room via websocket. In order to connect to a different room, the user has to create a new websocket connection. Maintaining multiple connections is expensive. The server may exhaust the connection pool. So, there is a need to use a single connection and communicate across multiple rooms.
Also, the "is_online" field was placed on the RoomUser. This means that only the users connected to that particular room will know if a user is online. But, we should rather notify all the rooms a user is part of that the user is online.
New Implementation:
RoomsConsumer.unsubscribe_from_all_activities
method can be used to unsubscribe the user from listening to any events occurring on the websocket. This is called when the connection is closed - disconnected.Usage:
The consuming application should change the websocket connection url to "/ws/chat". As we now send events from multiple rooms while being connected to a single websocket, you should segregate the events by room id, before displaying them in the UI.
Refactoring not related to this PR's Intent
model_observer
decorator.