chg: [serializer] add support for phoenix version 2 serialization
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.
I added basic support for sending data.
@gallypette Is broadcast functionality working right now?
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 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)
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()
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 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 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.
I reopen this PR as it seems relevant after all.
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 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
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.