-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.mjs
More file actions
42 lines (37 loc) · 1.14 KB
/
Copy pathchat.mjs
File metadata and controls
42 lines (37 loc) · 1.14 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
import { WSServerPubSub } from '../../../src/node.js';
function authCallback(token) {
// return false to deny the connection
if (token != 'auth-token') return false; // replace with your auth logic
// On the auth callback, you can return metadata about the user
return {
color: `hsl(${Math.floor(Math.random() * 360)}, 100%, 50%)`,
};
}
const wsServer = new WSServerPubSub({
port: 8887,
origins: '*',
authCallback,
});
wsServer.addChannel('chat', {
// This hook is called before the message is broadcasted to the channel
// You can modify the message before it is sent to all subscribers
// 'user' contains the metadata of the user that published the message
hookPub: (msg, user) => ({
time: Date.now(),
user: 'Anon. ' + user.id.slice(0, 4),
color: user.color,
msg,
}),
});
// This is a bot that sends a message to the chat channel every 5 seconds
// The hook will not be called for this message
// (hookPub is only called for messages published by clients)
setInterval(() => {
wsServer.pub('chat', {
time: Date.now(),
user: 'Bot',
color: 'tomato',
msg: 'Bot message',
});
}, 5000);
wsServer.start();