-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
87 lines (79 loc) · 3.08 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Supabase Realtime Broadcast Example</title>
<style>
body { font-family: Arial, sans-serif; }
#message-container { border: 1px solid #ccc; padding: 10px; height: 300px; overflow-y: scroll; }
#message-input { width: 80%; }
#send-button { width: 18%; }
</style>
</head>
<body>
<h1>Supabase Realtime Broadcast Example</h1>
<div id="message-container"></div>
<input type="text" id="message-input" placeholder="Type a message">
<button id="send-button">Send</button>
<!-- Include Supabase JS library -->
<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js"></script>
<script>
// Replace with your own Supabase project details
const SUPABASE_URL = 'http://127.0.0.1:54321';
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0';
// Initialize Supabase client with a different variable name
const supabaseClient = supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
// Create a broadcast channel
const channel = supabaseClient.channel('af', {
config: {
broadcast: { self: true },
},
});
// Subscribe to broadcast messages
channel.on('broadcast', { event: 'message' }, (payload) => {
console.log('Received message:', payload);
const messageContainer = document.getElementById('message-container');
const messageElement = document.createElement('div');
messageElement.textContent = `Received: ${payload.payload.content}`;
messageContainer.appendChild(messageElement);
// Scroll to the bottom
messageContainer.scrollTop = messageContainer.scrollHeight;
});
// Subscribe to the channel
channel.subscribe((status) => {
if (status === 'SUBSCRIBED') {
console.log('Subscribed to channel');
}
});
// Send message when button is clicked
const sendButton = document.getElementById('send-button');
sendButton.addEventListener('click', () => {
const messageInput = document.getElementById('message-input');
const content = messageInput.value.trim();
if (content !== '') {
channel.send({
type: 'broadcast',
event: 'message',
payload: { content },
});
// Display the sent message
const messageContainer = document.getElementById('message-container');
const messageElement = document.createElement('div');
messageElement.textContent = `Sent: ${content}`;
messageElement.style.textAlign = 'right';
messageContainer.appendChild(messageElement);
// Scroll to the bottom
messageContainer.scrollTop = messageContainer.scrollHeight;
messageInput.value = '';
}
});
// Allow sending messages with Enter key
const messageInput = document.getElementById('message-input');
messageInput.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
sendButton.click();
}
});
</script>
</body>
</html>