realtime-py icon indicating copy to clipboard operation
realtime-py copied to clipboard

chg: [serializer] add support for phoenix version 2 serialization

Open gallypette opened this issue 2 years ago • 12 comments

A PR to be able to use this library with latest phoenix versions.

feature: support phoenix version 2 serialization (mostly turn {} in []) bugfix: socket.summary was broken default: set hearbeat to 30 seconds (javascript's version default)

I did not test whether I broke compatibility with version 1, so consider this as a request for comments.

gallypette avatar Jul 24 '23 15:07 gallypette

I added basic support for sending data.

gallypette avatar Oct 04 '23 10:10 gallypette

@gallypette Is broadcast functionality working right now?

MindsightsAI avatar Oct 20 '23 08:10 MindsightsAI

It is still a poc, but yes. I am a bit short on the documentation side but something like this should do to send packets:

from realtime.connection import Socket

if __name__ == "__main__":
    TOKEN = "your token
    URL = f"ws://127.0.0.1:4000/socket/websocket?token={TOKEN}&vsn=2.0.0"

    ss = Socket(URL)
    ss.connect()

    channel_s = ss.set_channel("your channel")
    channel_s.join()
    channel_s.send("your event", "this is my payload 1", None)

The library has to be converted to full async to be able to send/receive without getting stuck in the listen function.

gallypette avatar Oct 20 '23 08:10 gallypette

@gallypette i can't make it workable

    def test_function(payload):
        print(payload)   -> there error 'unmatched topic'

    t1 = s.set_channel("test function")
    t1.join()
    t1.on("test event", callback=test_function, ref=None)
    t1.send("test event", "Bla bla bla", None)

image

MindsightsAI avatar Oct 20 '23 10:10 MindsightsAI

I guess you don't match correctly on your topic name in your channel code. Something like this would do:

def join(_topic, _payload, socket) do
   {:ok, socket}
end

def handle_in("test event", payload, socket) do
  {:reply, {:ok, payload}, socket}
end

For listening for the answer is a bit tricky though (because of the while loop in listen) but it could work for a few message. You could react on any event:

channel_s.on(None, None, callback1)
t1.listen()

Get answer to your input:

t1.on(None, 1, callback1)
t1.send("test event", "this is my payload 1", 1)
t1.listen()

gallypette avatar Oct 20 '23 12:10 gallypette

I pushed an async version that is more suitable for send/receive patterns. It diverged significantly so don't merge that, the API is not compatible anymore.

gallypette avatar Oct 20 '23 21:10 gallypette

@gallypette Can we use broadcast events using your asynchronous branch(flowintel:master) with realtime server in saas version of supabase? Or we need implement some handlers at Phoenix?

MindsightsAI avatar Oct 24 '23 19:10 MindsightsAI

@MindsightsAI I was not aware that supabase realtime had this feature.

Going quickly through realtime-js source code for send and _push, it looks like sending a "broadcast" event should work out of the box using this branch.

gallypette avatar Oct 25 '23 08:10 gallypette

I reopen this PR as it seems relevant after all.

gallypette avatar Oct 26 '23 06:10 gallypette

I just found out about @maxbaluev and @karvetskiy 's fork and decided to merge their forks into this PR to keep track of their progress.

gallypette avatar Dec 01 '23 13:12 gallypette

@gallypette Hey. Thanks for noticing my PR. Want to notify you to be careful with sync callbacks because they are running blocking now. I think it is better to implement Thread execution logic if you want to use it

sevkar avatar Dec 02 '23 18:12 sevkar

Thanks for the work on this. Any news for the merge in the default branch? We recently saw https://github.com/bitnom/aiorealtime so maybe it's another option.

adulau avatar Feb 09 '24 07:02 adulau