joewalnes / reconnecting-websocket

A small decorator for the JavaScript WebSocket API that automatically reconnects
MIT License
4.21k stars 970 forks source link

Multiple Entries being saved in Database when using ReconnectingWebSocket with django-channels #115

Open pirateksh opened 3 years ago

pirateksh commented 3 years ago

I have made a simple Real-Time Chat Application using Django-channels.

I have made a consumer named ChatConsumer which inherits from AsyncConsumer.

It has methods _websocket_connect()_, _websocketreceive() and _websocket_disconnect()_.

Additionally, I have defined two more methods namely _get_thread() and create_chat_message()_ which interact with the database.

Among these methods, _create_chat_message() is the method that actually saves the chat message into the database. It is called from the websocket_receive()_ method.

The issue I am facing is that, when I use ReconnectingWebSocket, somehow the message received in the _websocketreceive() method is saved multiple times in the database.

However, if I use vanilla WebSocket, everything works fine.

I also have also posted about the same issue on StackOverflow which I encountered about a year ago when I was working on a different project. Here is the link to it.

I have been able to reproduce this bug two times in two different projects, so, I am pretty sure that it is somehow related to ReconnectingWebSocket.

pirateksh commented 3 years ago

I have observed one more thing about Multiple Entries Being saved. Number of Times the same value is saved in database == Number of times the WebSocket connection reconnects.