Coming from Firebase Realtime Database
Concept mapping
| Firebase | svelte-realtime |
|---|---|
ref.on('value', cb) | live.stream() with merge: 'set' |
ref.on('child_added', cb) | live.stream() with merge: 'crud', created events |
ref.on('child_changed', cb) | Same stream, updated events |
ref.on('child_removed', cb) | Same stream, deleted events |
ref.set(data) | live() RPC call |
ref.push() | RPC + ctx.publish(topic, 'created', data) |
ref.update(data) | RPC + ctx.publish(topic, 'updated', data) |
ref.remove() | RPC + ctx.publish(topic, 'deleted', data) |
| Security rules | upgrade() + guard() |
.orderByChild().limitToLast() | Cursor pagination with loadMore() |
onDisconnect() | onUnsubscribe lifecycle hook |
Side-by-side: Real-time list
Firebase
import { getDatabase, ref, onChildAdded, onChildRemoved, push, set } from 'firebase/database';
const db = getDatabase();
const todosRef = ref(db, 'todos');
onChildAdded(todosRef, (snapshot) => {
todos = [...todos, { id: snapshot.key, ...snapshot.val() }];
});
onChildRemoved(todosRef, (snapshot) => {
todos = todos.filter(t => t.id !== snapshot.key);
});
function addTodo(text) {
const newRef = push(todosRef);
set(newRef, { text, done: false });
} svelte-realtime
// src/live/todos.js
export const addTodo = live(async (ctx, text) => {
const todo = { id: crypto.randomUUID(), text, done: false };
ctx.publish('todos', 'created', todo);
return todo;
});
export const todos = live.stream('todos', () => [], {
merge: 'crud', key: 'id'
}); <script>
import { todos, addTodo } from '$live/todos';
</script>
{#each $todos as todo (todo.id)}
<p>{todo.text}</p>
{/each} Key differences
- No Firebase SDK. No Google dependency, no vendor lock-in. Runs on your own server.
- Server-side logic. Firebase security rules are JSON-based and limited. svelte-realtime uses real code -
guard()with full access to your database, auth system, and business logic. - No listener management. Firebase requires
on()/off()cleanup. svelte-realtime stores auto-subscribe and auto-cleanup on component destroy. - WebSocket, not long polling. Firebase falls back to long polling in some environments. svelte-realtime is always WebSocket.
Migration steps
- Replace
ref.on('value', cb)withlive.stream()subscriptions - Replace
ref.set()/push()/update()withlive()RPC handlers - Add
ctx.publish()after each write to broadcast changes - Replace Firebase security rules with
upgrade()+guard() - Replace
onDisconnect()withonUnsubscribelifecycle hooks
Was this page helpful?