Realtime example on local Next.js app not working

Hard to describe what's going on so I just recorded my screen while testing the realtime example Basically, it first crash, then the engine reloads, the connection seems alive, but I type, nothing's going on and at some point (very random) it register the typing The error in browser console is from iCloud Passwords extension, it can be ignored Registry:
import { actor, setup } from "rivetkit";
import * as Y from "yjs";
import { applyUpdate, encodeStateAsUpdate } from "yjs";

export const yjsDocument = actor({
// Persistent state that survives restarts.
state: {
docData: new Uint8Array(), // Raw Yjs document snapshot
lastModified: 0,
},

createVars: () => ({
doc: new Y.Doc(),
}),

onStart: (c) => {
if (c.state.docData.length > 0) {
applyUpdate(c.vars.doc, c.state.docData);
}
},

// Handle client connections.
onConnect: (c, conn) => {
const update = encodeStateAsUpdate(c.vars.doc);
conn.send("initialState", { update });
},

actions: {
// Callable functions from clients.
applyUpdate: (c, update: Uint8Array) => {
applyUpdate(c.vars.doc, update);

const fullState = encodeStateAsUpdate(
c.vars.doc,
) as Uint8Array<ArrayBuffer>;
// State changes are automatically persisted
c.state.docData = fullState;
c.state.lastModified = Date.now();

// Send events to all connected clients.
c.broadcast("update", { update });
},

getState: (c) => ({
docData: c.state.docData,
lastModified: c.state.lastModified,
}),
},
});

// Register actors for use.
export const registry = setup({
use: { yjsDocument },
});
import { actor, setup } from "rivetkit";
import * as Y from "yjs";
import { applyUpdate, encodeStateAsUpdate } from "yjs";

export const yjsDocument = actor({
// Persistent state that survives restarts.
state: {
docData: new Uint8Array(), // Raw Yjs document snapshot
lastModified: 0,
},

createVars: () => ({
doc: new Y.Doc(),
}),

onStart: (c) => {
if (c.state.docData.length > 0) {
applyUpdate(c.vars.doc, c.state.docData);
}
},

// Handle client connections.
onConnect: (c, conn) => {
const update = encodeStateAsUpdate(c.vars.doc);
conn.send("initialState", { update });
},

actions: {
// Callable functions from clients.
applyUpdate: (c, update: Uint8Array) => {
applyUpdate(c.vars.doc, update);

const fullState = encodeStateAsUpdate(
c.vars.doc,
) as Uint8Array<ArrayBuffer>;
// State changes are automatically persisted
c.state.docData = fullState;
c.state.lastModified = Date.now();

// Send events to all connected clients.
c.broadcast("update", { update });
},

getState: (c) => ({
docData: c.state.docData,
lastModified: c.state.lastModified,
}),
},
});

// Register actors for use.
export const registry = setup({
use: { yjsDocument },
});
5 Replies
Nathan
Nathan2mo ago
hey! we've been working out some of these issues with local dev, we're cutting a release today or tomorrow that should solve this will try to remmeber to post here when it's up @Baptiste we've fixed a handful of issues in v2.0.22, give it a try!
Baptiste
BaptisteOP2mo ago
I can make the default Next.js count example work though not the realtime doc one. Do you confirm the Realtime docs example works on your end? Now it seems the increment count example does not work either
level=DEBUG msg="detected development environment, auto-starting engine and auto-configuring serverless"
level=DEBUG msg="disabling health check since using serverless"
level=DEBUG msg="run engine requested" version=2.0.22
level=DEBUG msg="ensuring engine process" version=2.0.22
level=DEBUG msg="using rivet engine driver" endpoint=http://localhost:6420
level=INFO msg="rivetkit ready" driver=engine definitions=1
level=DEBUG msg="engine binary already cached" version=2.0.22 path="/Users/baptistearno/Library/Application Support/rivetkit/builder-93d1c0ed/bin/rivet-engine-2.0.22"
level=DEBUG msg="rivet engine already running on port" port=6420
level=DEBUG msg="waiting for engine health check"
level=DEBUG msg="engine health check passed"
level=DEBUG msg="engine already running and healthy" version=2.0.22
level=DEBUG msg="configuring serverless runner"
level=DEBUG msg="fetching datacenters" endpoint=http://localhost:6420
level=DEBUG msg="making api call" method=GET url="http://localhost:6420/datacenters?namespace=default"
level=DEBUG msg="sending http request" url="http://localhost:6420/datacenters?namespace=default" encoding=json
level=DEBUG msg="configuring serverless runner" runnerName=default namespace=default
level=DEBUG msg="making api call" method=PUT url="http://localhost:6420/runner-configs/default?namespace=default"
level=DEBUG msg="sending http request" url="http://localhost:6420/runner-configs/default?namespace=default" encoding=json
level=INFO msg="serverless runner configured successfully" runnerName=default namespace=default
level=DEBUG msg="checking for file changes" routePath=/Users/baptistearno/Dev/typebot.io/apps/builder/.next/server/app/api/rivet/[...all]/route.js
level=INFO msg="route file changed" routePath=/Users/baptistearno/Dev/typebot.io/apps/builder/.next/server/app/api/rivet/[...all]/route.js
level=DEBUG msg="SSE aborted, shutting down runner"
level=INFO msg="stopping engine actor driver"
level=INFO msg="starting shutdown" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00 immediate=false exit=false
level=INFO msg="sending stopping message" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00 readyState=1
level=INFO msg="closing WebSocket" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00
level=INFO msg="connection closed" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00 code=1006 reason=""
level=WARN msg="runner disconnected" namespace=default runnerName=default code=1006 reason=""
level=INFO msg="connection closed" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00 code=1006 reason=""
level=INFO msg="websocket shutdown completed" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00
level=DEBUG msg="runner is stopped"
level=DEBUG msg="stream completed"
level=DEBUG msg="clearing file watcher interval: stream finished"
level=DEBUG msg="detected development environment, auto-starting engine and auto-configuring serverless"
level=DEBUG msg="disabling health check since using serverless"
level=DEBUG msg="run engine requested" version=2.0.22
level=DEBUG msg="ensuring engine process" version=2.0.22
level=DEBUG msg="using rivet engine driver" endpoint=http://localhost:6420
level=INFO msg="rivetkit ready" driver=engine definitions=1
level=DEBUG msg="engine binary already cached" version=2.0.22 path="/Users/baptistearno/Library/Application Support/rivetkit/builder-93d1c0ed/bin/rivet-engine-2.0.22"
level=DEBUG msg="rivet engine already running on port" port=6420
level=DEBUG msg="waiting for engine health check"
level=DEBUG msg="engine health check passed"
level=DEBUG msg="engine already running and healthy" version=2.0.22
level=DEBUG msg="configuring serverless runner"
level=DEBUG msg="fetching datacenters" endpoint=http://localhost:6420
level=DEBUG msg="making api call" method=GET url="http://localhost:6420/datacenters?namespace=default"
level=DEBUG msg="sending http request" url="http://localhost:6420/datacenters?namespace=default" encoding=json
level=DEBUG msg="configuring serverless runner" runnerName=default namespace=default
level=DEBUG msg="making api call" method=PUT url="http://localhost:6420/runner-configs/default?namespace=default"
level=DEBUG msg="sending http request" url="http://localhost:6420/runner-configs/default?namespace=default" encoding=json
level=INFO msg="serverless runner configured successfully" runnerName=default namespace=default
level=DEBUG msg="checking for file changes" routePath=/Users/baptistearno/Dev/typebot.io/apps/builder/.next/server/app/api/rivet/[...all]/route.js
level=INFO msg="route file changed" routePath=/Users/baptistearno/Dev/typebot.io/apps/builder/.next/server/app/api/rivet/[...all]/route.js
level=DEBUG msg="SSE aborted, shutting down runner"
level=INFO msg="stopping engine actor driver"
level=INFO msg="starting shutdown" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00 immediate=false exit=false
level=INFO msg="sending stopping message" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00 readyState=1
level=INFO msg="closing WebSocket" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00
level=INFO msg="connection closed" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00 code=1006 reason=""
level=WARN msg="runner disconnected" namespace=default runnerName=default code=1006 reason=""
level=INFO msg="connection closed" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00 code=1006 reason=""
level=INFO msg="websocket shutdown completed" runnerId=hwv2et7fazo2y45k07tb9ukbtccl00
level=DEBUG msg="runner is stopped"
level=DEBUG msg="stream completed"
level=DEBUG msg="clearing file watcher interval: stream finished"
Baptiste
BaptisteOP2mo ago
No description
Nathan
Nathan2mo ago
looking in to this in a few
Nathan
Nathan4w ago
i'm sorry this fell off my plate. it looks like you're self-hosting Rivet and connecting it to Next.js correct? if this is running on localhost, something's not right. If you're running this on a separate server from Next, you need to configure the public_url: https://github.com/rivet-dev/rivet/blob/21c1ff44bae64d80cde5846c5ab937d0e9e8ba7b/engine/packages/config/src/config/topology.rs#L73
GitHub
rivet/engine/packages/config/src/config/topology.rs at 21c1ff44bae6...
An open-source library for long-lived processes with realtime, persistence, and hibernation - rivet-dev/rivet

Did you find this page helpful?