Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket disconnect after a while #85

Open
Sapessii opened this issue Jan 26, 2024 · 3 comments
Open

Socket disconnect after a while #85

Sapessii opened this issue Jan 26, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@Sapessii
Copy link

Hi!

I am facing an issue both in local (Mac OS Sonoma 14.2) and deployed on render.com.

Basically after a while the socket close without receiving further updates and I get this error:

2024-01-26 04:04:08,363:ERROR - Connection with the server closed.
Traceback (most recent call last):
File "/opt/render/project/src/.venv/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 959, in transfer_data
message = await self.read_message()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/render/project/src/.venv/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 1029, in read_message
frame = await self.read_data_frame(max_size=self.max_size)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/render/project/src/.venv/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 1104, in read_data_frame
frame = await self.read_frame(max_size)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/render/project/src/.venv/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 1161, in read_frame
frame = await Frame.read(
^^^^^^^^^^^^^^^^^
File "/opt/render/project/src/.venv/lib/python3.11/site-packages/websockets/legacy/framing.py", line 68, in read
data = await reader(2)
^^^^^^^^^^^^^^^
File "/opt/render/project/python/Python-3.11.0/lib/python3.11/asyncio/streams.py", line 726, in readexactly
raise exceptions.IncompleteReadError(incomplete, n)
asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 2 expected bytes
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/render/project/src/.venv/lib/python3.11/site-packages/realtime/connection.py", line 76, in _listen
msg = await self.ws_connection.recv()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/render/project/src/.venv/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 568, in recv
await self.ensure_open()
File "/opt/render/project/src/.venv/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 930, in ensure_open
raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: no close frame received or sent

To Reproduce

  1. Launch the server with python main.py
  2. Wait a while without receiving updates on the socket

Expected behavior

The socket should't disconnect.

Additional context

This is my current python code

if __name__ == "__main__":
    URL = f"wss://{os.getenv('SUPABASE_ID')}.supabase.co/realtime/v1/websocket?apikey={os.getenv('SUPABASE_SERVICE_KEY')}&vsn=1.0.0"
    s = Socket(URL)
    s.connect()

    channel_1 = s.set_channel("realtime:public:todos")
    channel_1.join().on("INSERT", lambda msg: asyncio.ensure_future(callback(msg)))

    s.listen()
@Sapessii Sapessii added the bug Something isn't working label Jan 26, 2024
@chrisgoddard
Copy link

Having the same issue -- any updates or workarounds on this?

@loner233
Copy link

loner233 commented Aug 7, 2024

Same issue with me.

@yorickvanzweeden
Copy link
Contributor

I think it comes down to:

  • There will always be a disconnect when working with websockets
  • You should auto_reconnect (client option).
  • The auto_reconnect should rejoin channels

However, the auto_reconnect does not seem work properly for me and ignores new events after reconnect. I fixed that behavior in an opinionated fork. This fork does have breaking changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants