supabase-community / realtime-csharp

A C# client library for supabase/realtime.
https://supabase-community.github.io/realtime-csharp/api/Supabase.Realtime.Client.html
MIT License
70 stars 12 forks source link

Misplaced `type` property in realtime messages? #36

Closed aurecchia closed 7 months ago

aurecchia commented 1 year ago

Bug report

Hello folks. I encountered a problem and after some investigation I think there might be a slight problem with how realtime messages are serialized when being sent to sockets.

Describe the bug

Messages are serialized incorrectly when being broadcast. Specifically, the type property is "in the wrong place", which causes messages not to be picked up by subscribers.

I had the issue when using the official JavaScript library on the subscriber side.

To Reproduce

Publish a message as follows:

let payload = {| playerId = playerId |}
let! _ = channel.Send(Constants.ChannelEventName.Broadcast, "joined", payload)

which will be mapped onto the SocketRequest type and serialized to JSON (with the default encoder) as:

{
  "type": "joined",
  "topic": "realtime:35dc5021-8f5b-4303-95e7-01e0ddbff781",
  "event": "broadcast",
  "payload": {
    "playerId": "0d492920-caa5-4a88-81d5-0d7c4ae4e77b"
  },
  "ref": "313bd635-a779-4c42-8406-6684f6906095"
}

On the receiving end, set up like so:

supabase
    .channel(lobbyId)
    .on('broadcast', { event: 'joined' }, payload => {
        console.log("Player joined", payload);
    })
    .subscribe();

nothing will happen, as the JavaScript library expects the type property to be part of payload, as described on the Broadcast Protocol page.

Manually adding a type property as part of the payload causes the message to be processed correctly on the subscriber side.

Interestingly, if I inspect the WebSockets connection in the browser, the type property is not even present:

image

Expected behavior

It should be possible to consume messages from the JavaScript library.

System information

aurecchia commented 1 year ago

Sorry for the double-post. Looking again at the broadcast protocol, it seems I might just be misunderstanding what the type argument of the Send method is supposed to represent.

If that has nothing to do with the event property that subscribers are supposed to be using for listening to specific messages, then this bug report is completely wrong.

acupofjose commented 1 year ago

@aurecchia sorry for the delay getting to this one. I think you're right with the broadcast message type, specifically here: https://supabase.com/docs/guides/realtime/protocol#broadcast-message. The API has changed over time and this has probably been overlooked. I'll need to poke around a bit with the api - You're welcome to do a PR if you like!

aurecchia commented 1 year ago

No worries, there was no need to rush :)

I wasn't entirely sure, because the names used in the protocol and in the library are slightly different. If I find a bit of time, I'll give a go at putting together a fix 👍