-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathregistry.ts
More file actions
40 lines (33 loc) · 1007 Bytes
/
registry.ts
File metadata and controls
40 lines (33 loc) · 1007 Bytes
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
import { worker, setup } from "rivetkit";
// state managed by the actor
export interface State {
messages: { username: string; message: string }[];
}
export const chatRoom = worker({
// initialize state
state: { messages: [] } as State,
// define actions
actions: {
// receive an action call from the client
sendMessage: (c, username: string, message: string) => {
// save message to persistent storage
c.state.messages.push({ username, message });
// broadcast message to all clients
c.broadcast("newMessage", username, message);
},
getHistory: (c) => {
return c.state.messages;
},
},
});
// Create and export the app
export const registry = setup({
workers: { chatRoom },
cors: {
origin: "*", // Allow all origins
allowMethods: ["GET", "POST", "OPTIONS"], // Allow specific methods
allowHeaders: ["Content-Type", "Authorization", "User-Agent"], // Allow specific headers
},
});
// Export type for client type checking
export type Registry = typeof registry;