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

Using realtime.listen() in a separate thread #66

Open
lschaupp opened this issue Jul 18, 2023 · 3 comments
Open

Using realtime.listen() in a separate thread #66

lschaupp opened this issue Jul 18, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@lschaupp
Copy link

Hello,

Is it possible to use the realtime.listen() function in a separate thread?
All my trials have failed so far causing following exception:

raise RuntimeError('There is no current event loop in thread %r.'

at the line: loop = asyncio.get_event_loop() # TODO: replace with get_running_loop in connection.py

Cheers

@lschaupp lschaupp added the bug Something isn't working label Jul 18, 2023
@albertpurnama
Copy link

I'm interested in working on this. But I'm not sure where to start, I'm thinking of exposing the internal functions like _connect(), etc for use somehow.

I might be very off here, is there someone that I can talk to about the high level implementation on this?

Maybe @silentworks have some insights?

@albertpurnama
Copy link

albertpurnama commented Apr 19, 2024

For my case, running realtime with existing web server (FastAPI) crashes because there's already event loop running managed by FastAPI.

RuntimeError: this event loop is already running.

The workaround for me is to use the _connect() and _listen() and _join directly.

async def start():
    URL = f'wss://{os.getenv("SUPABASE_ID", "")}.supabase.co/realtime/v1/websocket?apikey={os.getenv("SUPABASE_SERVICE_ROLE_KEY", "")}&vsn=1.0.0'

    s = Socket(URL)
    # s.connect()
    await s._connect()

    channel_1 = cast(Channel, s.set_channel("realtime:storage:objects"))
    
    # channel_1.join().on("UPDATE", reindex)
    await asyncio.create_task(channel_1._join())
    channel_1.on("UPDATE", reindex)

    #s.listen()
    asyncio.create_task(s._listen())
    asyncio.create_task(s._keep_alive())

I wonder what the # TODO: replace with get_running_loop is supposed to mean 🤔

I tried replacing get_event_loop to get_running_loop but it doesn't work for my case. Probably skill issues. Guidance appreciated!

cc @J0

@regregoff
Copy link

Hello,

Is it possible to use the realtime.listen() function in a separate thread? All my trials have failed so far causing following exception:

raise RuntimeError('There is no current event loop in thread %r.'

at the line: loop = asyncio.get_event_loop() # TODO: replace with get_running_loop in connection.py

Cheers

To solve this problem, you need to create and run an event loop inside the function, which will be executed in a separate thread.

def socket_listen()
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    s = Socket(URL)
    s.connect()
    channel_1 = s.set_channel("realtime:*")
    s.listen()

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

3 participants