Skip to content

Docs for broadcasting messages don't explain that channels need to be cleaned up #281

Open
@mqp

Description

@mqp

Improve documentation

Link

https://supabase.com/docs/reference/javascript/broadcastmessage?example=send-a-message

Describe the problem

If you follow the instructions in the documentation to send a broadcast message over HTTP:

supabase.channel('room1').send({
  type: 'broadcast',
  event: 'cursor-pos',
  payload: { x: Math.random(), y: Math.random()
}})

supabase.channel will create a big puffy RealtimeChannel object and append it to the RealtimeClient.channels array, and never clean it up. So if you just ran this code in your app whenever you wanted to send a message the array would just keep growing full of channels without bounds.

Since I was about to implement a pattern like "our API server broadcasts a message every time new stuff happens", this would mean my API server would have a big memory leak, which would be very bad.

Describe the improvement

You can clean up the channel by calling RealtimeClient.removeChannel, although the code inside removeChannel is very silly looking in the case when the channel isn't connected (it constructs a fake leave push to unsubscribe and then triggers a fake OK on it.)

const chan = supabase.channel('room1');
await chan.send({ /* ... */ });
await supabase.removeChannel(chan)

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions