joewalnes / reconnecting-websocket

A small decorator for the JavaScript WebSocket API that automatically reconnects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

pirateksh opened this issue · comments

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(), websocket_receive() 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 websocket_receive() 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.

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.