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

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

Open
wants to merge 37 commits into
base: main
Choose a base branch
from

Conversation

gallypette
Copy link

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
Copy link
Author

I added basic support for sending data.

@MindsightsAI
Copy link

@gallypette Is broadcast functionality working right now?

@gallypette
Copy link
Author

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.

@MindsightsAI
Copy link

MindsightsAI commented Oct 20, 2023

@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

@gallypette
Copy link
Author

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
Copy link
Author

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 gallypette closed this Oct 20, 2023
@MindsightsAI
Copy link

MindsightsAI commented Oct 24, 2023

@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?

@gallypette
Copy link
Author

@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
Copy link
Author

I reopen this PR as it seems relevant after all.

@silentworks silentworks self-requested a review November 13, 2023 19:01
@silentworks silentworks self-assigned this Nov 13, 2023
@gallypette
Copy link
Author

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.

@karvetskiy
Copy link

@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

@adulau
Copy link

adulau commented Feb 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants