Skip to content

Advanced Techniques with @briklab/slokr

Wait for Startup Explicitly

ts
const server = new Slokr(Slokr.Hybrid, 3000);

try {
  await server.connected;
} catch (error) {
  console.error("Startup failed", error);
}

Use any for Cross-cutting Logging

ts
server.on("any", (payload, client) => {
  console.log("event from", client.client.from, payload);
});

Room-level Event Filtering

ts
server
  .rooms("team:red", "team:blue")
  .except("team:blue")
  .on("score:update", (payload) => {
    console.log("only red room updates", payload);
  });

Graceful Shutdown

ts
process.on("SIGTERM", () => {
  server.close();
  process.exit(0);
});

Operational Notes

  • connection and close events are emitted with Slokr.Client context.
  • broadcast payloads must not include SLIKR_INTERNAL_EVENT key.
  • WebSocket message handling includes rate-limit checks in internal bindings.