apps/docs/content/guides/realtime/getting_started.mdx
<Tabs scrollable size="small" type="underlined" defaultActiveId="ts" queryGroup="language"
<TabPanel id="ts" label="TypeScript">
npm install @supabase/supabase-js
flutter pub add supabase_flutter
let package = Package(
// ...
dependencies: [
// ...
.package(
url: "https://github.com/supabase/supabase-swift.git",
from: "2.0.0"
),
],
targets: [
.target(
name: "YourTargetName",
dependencies: [
.product(
name: "Supabase",
package: "supabase-swift"
),
]
)
]
)
pip install supabase
conda install -c conda-forge supabase
Get your project URL and key. <$Partial path="api_settings.mdx" variables={{ "framework": "", "tab": "" }} />
<Tabs scrollable size="small" type="underlined" defaultActiveId="ts" queryGroup="language"
<TabPanel id="ts" label="TypeScript">
import { createClient } from '@supabase/supabase-js'
const supabase = createClient('https://<project>.supabase.co', '<anon_key or sb_publishable_key>')
import 'package:supabase_flutter/supabase_flutter.dart';
void main() async {
await Supabase.initialize(
url: 'https://<project>.supabase.co',
anonKey: '<anon_key or sb_publishable_key>',
);
runApp(MyApp());
}
final supabase = Supabase.instance.client;
import Supabase
let supabase = SupabaseClient(
supabaseURL: URL(string: "https://<project>.supabase.co")!,
supabaseKey: "<anon_key or sb_publishable_key>"
)
from supabase import create_client, Client
url: str = "https://<project>.supabase.co"
key: str = "<anon_key or sb_publishable_key>"
supabase: Client = create_client(url, key)
Channels are the foundation of Realtime. Think of them as rooms where clients can communicate. Each channel is identified by a topic name and if they are public or private.
<Tabs scrollable size="small" type="underlined" defaultActiveId="ts" queryGroup="language"
<TabPanel id="ts" label="TypeScript">
// Create a channel with a descriptive topic name
const channel = supabase.channel('room:lobby:messages', {
config: { private: true }, // Recommended for production
})
// Create a channel with a descriptive topic name
final channel = supabase.channel('room:lobby:messages');
// Create a channel with a descriptive topic name
let channel = supabase.channel("room:lobby:messages") {
$0.isPrivate = true
}
# Create a channel with a descriptive topic name
channel = supabase.channel('room:lobby:messages', params={'config': {'private': True }})
Since we're using a private channel, you need to create a basic RLS policy on the realtime.messages table to allow authenticated users to connect. Row Level Security (RLS) policies control who can access your Realtime channels based on user authentication and custom rules:
-- Allow authenticated users to receive broadcasts
CREATE POLICY "authenticated_users_can_receive" ON realtime.messages
FOR SELECT TO authenticated USING (true);
-- Allow authenticated users to send broadcasts
CREATE POLICY "authenticated_users_can_send" ON realtime.messages
FOR INSERT TO authenticated WITH CHECK (true);
There are three main ways to send messages with Realtime:
Send and receive messages using the Supabase client:
<Tabs scrollable size="small" type="underlined" defaultActiveId="ts" queryGroup="language"
<TabPanel id="ts" label="TypeScript">
// Listen for messages
channel
.on('broadcast', { event: 'message_sent' }, (payload: { payload: any }) => {
console.log('New message:', payload.payload)
})
.subscribe()
// Send a message
channel.send({
type: 'broadcast',
event: 'message_sent',
payload: {
text: 'Hello, world!',
user: 'john_doe',
timestamp: new Date().toISOString(),
},
})
// Listen for messages
channel.onBroadcast(
event: 'message_sent',
callback: (payload) {
print('New message: ${payload['payload']}');
},
).subscribe();
// Send a message
channel.sendBroadcastMessage(
event: 'message_sent',
payload: {
'text': 'Hello, world!',
'user': 'john_doe',
'timestamp': DateTime.now().toIso8601String(),
},
);
// Listen for messages
await channel.onBroadcast(event: "message_sent") { message in
print("New message: \(message.payload)")
}
let status = await channel.subscribe()
// Send a message
await channel.sendBroadcastMessage(
event: "message_sent",
payload: [
"text": "Hello, world!",
"user": "john_doe",
"timestamp": ISO8601DateFormatter().string(from: Date())
]
)
# Listen for messages
def message_handler(payload):
print(f"New message: {payload['payload']}")
channel.on_broadcast(event="message_sent", callback=message_handler).subscribe()
# Send a message
channel.send_broadcast_message(
event="message_sent",
payload={
"text": "Hello, world!",
"user": "john_doe",
"timestamp": datetime.now().isoformat()
}
)
Send messages via HTTP requests, perfect for server-side applications:
<Tabs scrollable size="small" type="underlined" defaultActiveId="ts" queryGroup="language"
<TabPanel id="ts" label="TypeScript">
// Send message via REST API
const response = await fetch(`https://<project>.supabase.co/rest/v1/rpc/broadcast`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer <your-service-role-key>`,
apikey: '<your-service-role-key>',
},
body: JSON.stringify({
topic: 'room:lobby:messages',
event: 'message_sent',
payload: {
text: 'Hello from server!',
user: 'system',
timestamp: new Date().toISOString(),
},
private: true,
}),
})
import 'package:http/http.dart' as http;
import 'dart:convert';
// Send message via REST API
final response = await http.post(
Uri.parse('https://<project>.supabase.co/rest/v1/rpc/broadcast'),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer <your-service-role-key>',
'apikey': '<your-service-role-key>',
},
body: jsonEncode({
'topic': 'room:lobby:messages',
'event': 'message_sent',
'payload': {
'text': 'Hello from server!',
'user': 'system',
'timestamp': DateTime.now().toIso8601String(),
},
'private': true,
}),
);
import Foundation
// Send message via REST API
let url = URL(string: "https://<project>.supabase.co/rest/v1/rpc/broadcast")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer <your-service-role-key>", forHTTPHeaderField: "Authorization")
request.setValue("<your-service-role-key>", forHTTPHeaderField: "apikey")
let payload = [
"topic": "room:lobby:messages",
"event": "message_sent",
"payload": [
"text": "Hello from server!",
"user": "system",
"timestamp": ISO8601DateFormatter().string(from: Date())
],
"private": true
] as [String: Any]
request.httpBody = try JSONSerialization.data(withJSONObject: payload)
let (data, response) = try await URLSession.shared.data(for: request)
import requests
from datetime import datetime
# Send message via REST API
response = requests.post(
'https://<project>.supabase.co/rest/v1/rpc/broadcast',
headers={
'Content-Type': 'application/json',
'Authorization': 'Bearer <your-service-role-key>',
'apikey': '<your-service-role-key>'
},
json={
'topic': 'room:lobby:messages',
'event': 'message_sent',
'payload': {
'text': 'Hello from server!',
'user': 'system',
'timestamp': datetime.now().isoformat()
},
'private': True
}
)
Automatically broadcast database changes using triggers. Choose the approach that best fits your needs:
Using realtime.broadcast_changes (Best for mirroring database changes)
-- Create a trigger function for broadcasting database changes
CREATE OR REPLACE FUNCTION broadcast_message_changes()
RETURNS TRIGGER AS $$
BEGIN
-- Broadcast to room-specific channel
PERFORM realtime.broadcast_changes(
'room:' || NEW.room_id::text || ':messages',
TG_OP,
TG_OP,
TG_TABLE_NAME,
TG_TABLE_SCHEMA,
NEW,
OLD
);
RETURN NULL;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Apply trigger to your messages table
CREATE TRIGGER messages_broadcast_trigger
AFTER INSERT OR UPDATE OR DELETE ON messages
FOR EACH ROW EXECUTE FUNCTION broadcast_message_changes();
Using realtime.send (Best for custom notifications and filtered data)
-- Create a trigger function for custom notifications
CREATE OR REPLACE FUNCTION notify_message_activity()
RETURNS TRIGGER AS $$
BEGIN
-- Send custom notification when new message is created
IF TG_OP = 'INSERT' THEN
PERFORM realtime.send(
jsonb_build_object(
'message_id', NEW.id,
'user_id', NEW.user_id,
'room_id', NEW.room_id,
'created_at', NEW.created_at
),
'message_created',
'room:' || NEW.room_id::text || ':notifications',
true -- private channel
);
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Apply trigger to your messages table
CREATE TRIGGER messages_notification_trigger
AFTER INSERT ON messages
FOR EACH ROW EXECUTE FUNCTION notify_message_activity();
realtime.broadcast_changes sends the full database change with metadatarealtime.send allows you to send custom payloads and control exactly what data is broadcastAlways use private channels for production applications to ensure proper security and authorization:
const channel = supabase.channel('room:123:messages', {
config: { private: true },
})
Channel Topics: Use the pattern scope:id:entity
room:123:messages - Messages in room 123game:456:moves - Game moves for game 456user:789:notifications - Notifications for user 789Always unsubscribe when you are done with a channel to ensure you free up resources:
<Tabs scrollable size="small" type="underlined" defaultActiveId="ts" queryGroup="language"
<TabPanel id="ts" label="TypeScript">
// React example
import { useEffect } from 'react'
useEffect(() => {
const channel = supabase.channel('room:123:messages')
return () => {
supabase.removeChannel(channel)
}
}, [])
// Flutter example
class _MyWidgetState extends State<MyWidget> {
RealtimeChannel? _channel;
@override
void initState() {
super.initState();
_channel = supabase.channel('room:123:messages');
}
@override
void dispose() {
_channel?.unsubscribe();
super.dispose();
}
}
// SwiftUI example
struct ContentView: View {
@State private var channel: RealtimeChannelV2?
var body: some View {
// Your UI here
.onAppear {
channel = supabase.realtimeV2.channel("room:123:messages")
}
.onDisappear {
Task {
await channel?.unsubscribe()
}
}
}
}
# Python example with context manager
class RealtimeManager:
def __init__(self):
self.channel = None
def __enter__(self):
self.channel = supabase.channel('room:123:messages')
return self.channel
def __exit__(self, exc_type, exc_val, exc_tb):
if self.channel:
self.channel.unsubscribe()
# Usage
with RealtimeManager() as channel:
# Use channel here
pass
Now that you understand the basics, dive deeper into each feature:
Ready to build something amazing? Start with the Broadcast guide to create your first real-time feature!