-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathbroadcast-channel-chat.html
More file actions
447 lines (384 loc) · 13.1 KB
/
broadcast-channel-chat.html
File metadata and controls
447 lines (384 loc) · 13.1 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multi-Tab Sync Chat</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
border-radius: 20px;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 500px;
height: 600px;
display: flex;
flex-direction: column;
overflow: hidden;
}
.header {
background: linear-gradient(45deg, #ff6b6b, #ffa726);
color: white;
padding: 20px;
text-align: center;
position: relative;
}
.header h1 {
font-size: 1.8em;
margin-bottom: 8px;
}
.header p {
opacity: 0.9;
font-size: 0.9em;
}
.status {
position: absolute;
top: 15px;
right: 20px;
display: flex;
align-items: center;
gap: 8px;
font-size: 0.8em;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #4caf50;
animation: pulse 2s infinite;
}
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.5; }
100% { opacity: 1; }
}
.demo-info {
background: #e3f2fd;
border-left: 4px solid #2196f3;
padding: 15px;
margin: 20px;
border-radius: 8px;
font-size: 0.9em;
line-height: 1.4;
}
.chat-container {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
.messages {
flex: 1;
overflow-y: auto;
padding: 20px;
scroll-behavior: smooth;
}
.message {
background: #f5f5f5;
padding: 12px 16px;
border-radius: 18px;
margin-bottom: 12px;
max-width: 80%;
word-wrap: break-word;
animation: slideIn 0.3s ease-out;
position: relative;
}
.message.own {
background: linear-gradient(45deg, #667eea, #764ba2);
color: white;
margin-left: auto;
text-align: right;
}
.message.sync {
background: linear-gradient(45deg, #ff6b6b, #ffa726);
color: white;
border: 2px solid #fff;
box-shadow: 0 4px 12px rgba(255, 107, 107, 0.3);
}
.sync-indicator {
font-size: 0.7em;
opacity: 0.8;
margin-top: 4px;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.input-container {
padding: 20px;
background: rgba(255, 255, 255, 0.9);
border-top: 1px solid #eee;
}
.input-wrapper {
display: flex;
gap: 12px;
align-items: center;
}
#messageInput {
flex: 1;
padding: 12px 16px;
border: 2px solid #e0e0e0;
border-radius: 25px;
font-size: 16px;
outline: none;
transition: all 0.3s ease;
}
#messageInput:focus {
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
#sendButton {
background: linear-gradient(45deg, #667eea, #764ba2);
color: white;
border: none;
width: 48px;
height: 48px;
border-radius: 50%;
cursor: pointer;
font-size: 20px;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
}
#sendButton:hover {
transform: scale(1.1);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.3);
}
#sendButton:active {
transform: scale(0.95);
}
.empty-state {
text-align: center;
color: #999;
font-style: italic;
margin-top: 50px;
}
.tab-indicator {
position: absolute;
top: 15px;
left: 20px;
background: rgba(255, 255, 255, 0.2);
padding: 4px 8px;
border-radius: 12px;
font-size: 0.8em;
font-weight: bold;
}
@media (max-width: 600px) {
.container {
height: 100vh;
border-radius: 0;
max-width: none;
}
.demo-info {
margin: 10px;
padding: 10px;
font-size: 0.8em;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<div class="tab-indicator" id="tabId">Tab #1</div>
<div class="status">
<div class="status-dot"></div>
<span>Live Sync</span>
</div>
<h1>Multi-Tab Chat</h1>
<p>Messages sync instantly across all open tabs</p>
</div>
<div class="demo-info">
<strong>🚀 Demo Instructions:</strong> Open this page in multiple browser tabs, then type a message in any tab. Watch it appear instantly in all other tabs! No backend required - uses Broadcast Channel API.
</div>
<div class="chat-container">
<div class="messages" id="messages">
<div class="empty-state">
Start typing to see the magic happen! ✨
</div>
</div>
<div class="input-container">
<div class="input-wrapper">
<input
type="text"
id="messageInput"
placeholder="Type your message..."
maxlength="500"
>
<button id="sendButton" title="Send message">
➤
</button>
</div>
</div>
</div>
</div>
<script>
class MultiTabChat {
constructor() {
this.messages = [];
this.tabId = this.generateTabId();
this.channel = new BroadcastChannel('chat-sync');
this.init();
}
generateTabId() {
return Math.random().toString(36).substr(2, 9);
}
init() {
// Update tab indicator
document.getElementById('tabId').textContent = `Tab #${this.tabId.substr(0, 3)}`;
// Set up event listeners
this.setupEventListeners();
// Load existing messages from memory (if any)
this.loadMessages();
// Listen for messages from other tabs
this.channel.addEventListener('message', (event) => {
this.handleIncomingMessage(event.data);
});
// Send welcome message
setTimeout(() => {
this.addWelcomeMessage();
}, 500);
}
setupEventListeners() {
const input = document.getElementById('messageInput');
const button = document.getElementById('sendButton');
button.addEventListener('click', () => this.sendMessage());
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.sendMessage();
}
});
// Auto-focus input
input.focus();
}
sendMessage() {
const input = document.getElementById('messageInput');
const text = input.value.trim();
if (!text) return;
const message = {
id: Date.now() + '-' + this.tabId,
text: text,
timestamp: new Date(),
sender: this.tabId,
isOwn: true
};
// Add to local messages
this.messages.push(message);
this.renderMessage(message);
// Broadcast to other tabs
this.channel.postMessage({
type: 'new-message',
message: {
...message,
isOwn: false // Mark as external for other tabs
}
});
// Clear input
input.value = '';
input.focus();
// Scroll to bottom
this.scrollToBottom();
}
handleIncomingMessage(data) {
if (data.type === 'new-message') {
const message = {
...data.message,
isSync: true // Mark as synced message
};
// Avoid duplicates
if (!this.messages.find(m => m.id === message.id)) {
this.messages.push(message);
this.renderMessage(message);
this.scrollToBottom();
}
}
}
renderMessage(message) {
const messagesContainer = document.getElementById('messages');
// Remove empty state
const emptyState = messagesContainer.querySelector('.empty-state');
if (emptyState) {
emptyState.remove();
}
const messageEl = document.createElement('div');
messageEl.className = 'message';
if (message.isOwn) {
messageEl.classList.add('own');
} else if (message.isSync) {
messageEl.classList.add('sync');
}
const timeStr = new Date(message.timestamp).toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit'
});
messageEl.innerHTML = `
<div>${this.escapeHtml(message.text)}</div>
${message.isSync ? `<div class="sync-indicator">↻ Synced from Tab #${message.sender.substr(0, 3)}</div>` : ''}
`;
messagesContainer.appendChild(messageEl);
}
addWelcomeMessage() {
const welcomeMessage = {
id: 'welcome-' + this.tabId,
text: `🎉 Welcome! This is Tab #${this.tabId.substr(0, 3)}. Open more tabs to see real-time sync in action!`,
timestamp: new Date(),
sender: 'system',
isOwn: true
};
this.messages.push(welcomeMessage);
this.renderMessage(welcomeMessage);
this.scrollToBottom();
}
loadMessages() {
// In a real app, you might load from localStorage or indexedDB
// For this demo, we start fresh each time
}
scrollToBottom() {
const messagesContainer = document.getElementById('messages');
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
}
// Check if Broadcast Channel is supported
if ('BroadcastChannel' in window) {
// Initialize the chat app
const chat = new MultiTabChat();
} else {
// Fallback message for unsupported browsers
document.body.innerHTML = `
<div style="text-align: center; padding: 50px; font-family: Arial, sans-serif;">
<h2>Browser Not Supported</h2>
<p>This demo requires a browser that supports the Broadcast Channel API.</p>
<p>Please try Chrome, Firefox, or Safari.</p>
</div>
`;
}
</script>
</body>
</html>