I'm new to Django channels and I'm building a simple chatroom. With this chatroom, I want to disconnect a user if he closes the browser tab and if he reopens the tab with the same link, I want to reconnect him to the same chat that he was. How can I do that?
in order to reconnect to the same WebSocket, you just connect again to the same URL. From the client (browser) side you can use the example JavaScript code:
webSocket = new WebSocket('ws:/localhost:8000/ws/chat/');
The server side (the "backend", i.e., Django Channels) closes the connection when there are no clients connected.
What you probably mean is that you would like to have access to the chat history, yes? As I understand the WebSocket it is a communication protocol (in some sense similar to the HTTP). So it is used just to send the messages, not to store them. You should store the messages somewhere anyway, e.g., in the database. Then, on the "reconnect" you in fact get the messages from the database via views
, or, in a different fashion, with the consumers
.
Hope that helps. You would have to specify a concrete scenario for me to help you further.