supabase-community / realtime-py

A Python Client for Phoenix Channels
MIT License
118 stars 47 forks source link

Asynchronous API Alternatives #133

Open Maturin opened 1 month ago

Maturin commented 1 month ago

Feature request

Is your feature request related to a problem? Please describe.

I want to be able to have asynchronous tasks parallel to realtime-py. Basically the same mentioned @albertpurnama in a comment to #66 . While #66 is about using listen() in separate thread, this request and I guess the one from @albertpurnma, is to have an official public asynchronous API, instead of using private _ functions.

Describe the solution you'd like

I would like to add async siblings to the following functions:

This would the naming convention proposed here. I know this is no official document. But I couldn't find a better one.

The async function are basically a public wrapper around the private async functions.

Furthermore, I don't know if my list above is sufficient. Please extend the list. But it was sufficient, when I tried it some time ago.

Describe alternatives you've considered

Instead of creating a wrapper for the private async functions, they could be renamed.

But this would create more implementation effort for this ticket, and I like the idea of having a public function, which remains unchanged, even if internal stuff changes.

Additional context

n/a

albertpurnama commented 1 month ago

I like it.

So essentially, calling the internal functions like _join inside join_async. Will work for my case and seems like a simple change!

I tried to do fancy asyncio management stuff inside join itself to sort of handle scheduling to existing event loop, but I guess my understanding of how event loop works is still off by a mile.

Maturin commented 1 month ago

I am not an expert of the async Python programming myself. I believe it might be possible to get an event loop and use that. But that is basically, what the current join function does.

IMHO the biggest disadvantage of this approach that you turn a possibly async call into a synchronously call. Actually I wanted to do something like this

async def foo():
    s = Socket(url)
    await s.connect_async()
    # ...

for taking advantage of the asynchronous programming model.