Broadcast
Send and receive messages using Realtime Broadcast
Let's explore how to implement Realtime Broadcast to send messages between clients.
Usage#
You can use the Supabase client libraries to send and receive Broadcast messages.
Initialize the client#
Go to your Supabase project's API Settings and grab the URL
and anon
public API key.
Listening to Broadcast messages#
You can provide a callback for the broadcast
channel to receive message. In this example we will receive any broadcast
messages in room-1
:
Sending Broadcast messages#
We can send Broadcast messages using channelB.send()
. Let's set up another client to send messages.
Before sending messages we need to ensure the client is connected, which we have done within the subscribe()
callback.
Broadcast options#
You can pass configuration options while initializing the Supabase Client.
Self-send messages#
By default, broadcast messages are only sent to other clients. You can broadcast messages back to the sender by setting Broadcast's self
parameter to true
.
Acknowledge messages#
You can confirm that Realtime received your message by setting Broadcast's ack
config to true
.
Use this to guarantee that the server has received the message before resolving channelD.send
's promise. If the ack
config is not set to true
when creating the channel, the promise returned by channelD.send
will resolve immediately.
Send messages using REST calls#
note
This is currently available only in the Supabase JavaScript client version 2.37.0 and later.
You can also send a Broadcast message by making an HTTP request to Realtime servers. This is useful when you want to send messages from your server or client without having to first establish a WebSocket connection.