Realtime apps
Build a realtime app or multiplayer game with the omg.dev SDK and deploy it with omg deploy. Every change reaches every open browser live.
Declare your data in schema.ts and read it with useQuery. omg.dev gives
the app a database and an API, and sends every change to every open browser.
You do not write a server or WebSocket code.
Quickstart: a live message wall
1. Start a project
npx --yes create-omg@latest live-wall
cd live-wall2. Declare the data
import { defineSchema, collection, fields } from "@omg-dev/schema"
export default defineSchema({
collections: {
messages: collection({
fields: { text: fields.string() },
}).scoped("global"),
},
})Put schema.ts in the project root. .scoped("global") makes the data
shared by everyone. Without it, each signed-in user sees only their own rows.
3. Use it in React
import { useState } from "react"
import { useQuery } from "@omg-dev/sdk"
export default function App() {
const { data, create } = useQuery<{ id: string; text: string }>({
api: "/api/messages",
collection: "messages",
})
const [draft, setDraft] = useState("")
return (
<main>
<form
onSubmit={async (e) => {
e.preventDefault()
await create({ text: draft })
setDraft("")
}}
>
<input value={draft} onChange={(e) => setDraft(e.target.value)} />
</form>
<ul>{data.map((m) => <li key={m.id}>{m.text}</li>)}</ul>
</main>
)
}useQuery also returns update(id, changes) and remove(id).
4. Deploy
omg deployOpen the link in two browsers. A message posted in one shows in the other at once. First time? See Deploy with omg deploy.
Multiplayer games
Keep the shared game state in collections with .scoped("global"), for
example rooms and players. Every player reads them with useQuery. Put
rules such as "only the host starts the round" in a function.
Server rules (functions)
For anything useQuery does not cover, add a file in functions/:
// functions/search.ts
export async function POST(req: Request) {
const { q } = await req.json()
return Response.json({ results: [] })
}This serves /api/search. Export GET, POST, PUT, PATCH or DELETE.
A file named after a collection replaces its automatic create, update or
remove:
// functions/messages.ts: everyone reads messages, only the author deletes one
import { ctx, db, VibesHttpError } from "@omg-dev/server"
export async function remove(id: string) {
const row = await db.get("messages", id)
if (!row) throw new VibesHttpError(404, "Not found")
if (row.author !== ctx.userId) throw new VibesHttpError(403, "Only the author can delete this")
await db.delete("messages", id)
return { ok: true }
}Reference
- Schema reference: fields, scopes and indexes.
- useQuery reference: return values and live updates.
- Multi-user auth: sign-in and per-user data.