import{Actor,typeRpc}from"@rivet-gg/actor";class ChatRoom extendsActor{// receive an remote procedure call from the clientsendMessage(rpc: Rpc,username: string,message: string){// save message to persistent storagethis._state.messages.push({username,message});// broadcast message to all clientsthis._broadcast("newMessage",{username,message});}// initialize this._stateoverride_onInitialize(){return{messages:[]};}}
client.ts
Runs in the browser
import{Client}from"@rivet-gg/actor-client";importtype{Chat}from"./chat.ts";// connect to actorconstrivet = newClient(/* ... */);constroom = awaitrivet.get<ChatRoom>({name:"room",channel:"random",});// send a messageroom.sendMessage("william","All the world's a stage.");// receive broadcasted message from actorroom.on("newMessage",({username,message})=>{console.log(`${username}: ${message}`);});
Rivet comes packed with features
State & Persistence
State that feels like memory but works like storage. Ideal for dynamic, fast-moving apps.
import{Actor,typeRpc}from"@rivet-gg/actor";class ChatRoom extendsActor{// receive an remote procedure call from the clientsendMessage(rpc: Rpc,username: string,message: string){// save message to persistent storagethis._state.messages.push({username,message});// broadcast message to all clientsthis._broadcast("newMessage",{username,message});}// initialize this._stateoverride_onInitialize(){return{messages:[]};}}
client.ts
Runs in the browser
import{Client}from"@rivet-gg/actor-client";importtype{Chat}from"./chat.ts";// connect to actorconstrivet = newClient(/* ... */);constroom = awaitrivet.get<ChatRoom>({name:"room",channel:"random",});// send a messageroom.sendMessage("william","All the world's a stage.");// receive broadcasted message from actorroom.on("newMessage",({username,message})=>{console.log(`${username}: ${message}`);});
Rivet comes packed with features
State & Persistence
State that feels like memory but works like storage. Ideal for dynamic, fast-moving apps.
import{Actor,typeRpc}from"@rivet-gg/actor";class ChatRoom extendsActor{// receive an remote procedure call from the clientsendMessage(rpc: Rpc,username: string,message: string){// save message to persistent storagethis._state.messages.push({username,message});// broadcast message to all clientsthis._broadcast("newMessage",{username,message});}// initialize this._stateoverride_onInitialize(){return{messages:[]};}}
client.ts
Runs in the browser
import{Client}from"@rivet-gg/actor-client";importtype{Chat}from"./chat.ts";// connect to actorconstrivet = newClient(/* ... */);constroom = awaitrivet.get<ChatRoom>({name:"room",channel:"random",});// send a messageroom.sendMessage("william","All the world's a stage.");// receive broadcasted message from actorroom.on("newMessage",({username,message})=>{console.log(`${username}: ${message}`);});
Rivet comes packed with features
State & Persistence
State that feels like memory but works like storage. Ideal for dynamic, fast-moving apps.