PixelSyncServer
Authoritative server for pixel sync.
One instance manages one shared PixelBuffer under one namespace.
ts
import { defineConfig } from "vite";
import { createWebSocketNetworkPlugin } from "@jolly-pixel/network/plugins/vite.ts";
import {
PixelBuffer,
PixelSyncServer
} from "@jolly-pixel/pixel-draw.renderer";
const mainTexture = new PixelSyncServer({
namespace: "pixel-draw:main",
buffer: new PixelBuffer({
size: { x: 80, y: 80 }
})
});
export default defineConfig({
plugins: [
createWebSocketNetworkPlugin({
plugins: [mainTexture]
})
]
});Important Rules
- Do not reuse a namespace across different buffers.
- Pre-size the server buffer to match expected client startup state.
- Register one
PixelSyncServerper collaborative canvas.
What It Handles
- Sends a snapshot immediately when a client joins.
- Accepts incoming commands.
- Resolves conflicts.
- Applies accepted commands to authoritative buffer.
- Broadcasts accepted commands to clients in the same namespace.
Constructor Options
ts
new PixelSyncServer(options?: PixelSyncServerOptions)
interface PixelSyncServerOptions {
namespace?: string; // default: "pixel-draw"
buffer?: PixelBuffer; // default: blank 1x1
conflictResolver?: PixelConflictResolver; // default: LastWriteWinsResolver
}
interface PixelBufferSnapshot {
size: Vec2;
pixels: string; // base64 RGBA
uvRegions: UVRegion[];
}
type PixelServerMessage =
| { type: "snapshot"; data: PixelBufferSnapshot; }
| { type: "command"; data: PixelNetworkCommand; };Conflict Policy (Minimal)
By default, PixelSyncServer uses LastWriteWinsResolver.
What that means:
strokeandselect-editconflicts resolve per pixel.uv-region-movedanduv-region-deletedconflicts resolve per region id.resized,texture-replaced,global-fill, anduv-region-createdare always accepted.- For the same key, same-client commands are accepted in send order; otherwise newer
timestampwins (andclientIdbreaks ties).
You can override this with conflictResolver in PixelSyncServerOptions when you need custom behavior.
API You Might Actually Use
server.namespace: namespace key.server.buffer: authoritative buffer.server.receive(cmd): useful in tests and replay tools.server.snapshot(): exports currentPixelBufferSnapshot.
attach, onClientConnect, onClientDisconnect, and onMessage are NetworkPlugin lifecycle hooks invoked by @jolly-pixel/network.
Multi-Buffer Example
ts
createWebSocketNetworkPlugin({
plugins: [
new PixelSyncServer({
namespace: "pixel-draw:characters",
buffer: new PixelBuffer({ size: { x: 32, y: 32 } })
}),
new PixelSyncServer({
namespace: "pixel-draw:tiles",
buffer: new PixelBuffer({ size: { x: 128, y: 128 } })
})
]
});