Skip to Content
sock8 is still in very early development. The API is unstable and subject to change.
Channel DSLNesting and Structure

Nesting and Structure

Sock8 allows you to organize channels into nested structures.
This helps you group related channels together logically, rather than having a completely flat namespace.


Simple Nesting

You can nest channel definitions under static keys.

{ 'notifications': [ z.object({ title: z.string(), body: z.string() }), ], 'admin.logs': [ z.object({ message: z.string(), timestamp: z.number() }), ], }

This produces a channel tree:

channels.notifications.emit(...); channels.admin.logs.emit(...);

Notice how 'admin.logs' creates a nested structure automatically.


Nesting with Parameters

You can also nest parameterized channels.

{ 'chat.{roomId}.messages': [ z.object({ sender: z.string(), text: z.string() }), ], 'chat.{roomId}.typing': [ z.object({ userId: z.string() }), ], }

Both channels live under the same chat grouping —
and both share the {roomId} parameter.

Accessing them:

channels.chat.messages.for({ roomId: 'abc123' }).emit(...); channels.chat.typing.for({ roomId: 'abc123' }).subscribe(...);

Deep Nesting

You can nest channels multiple levels deep.

{ 'org.{orgId}.projects.{projectId}.updates': [ z.object({ title: z.string() }), ], }

Access:

channels.org.projects.updates.for({ orgId: 'org-1', projectId: 'proj-1' }).emit(...);

Sock8’s .for() method intelligently attaches at the right node,
based on the parameter structure of the path.


Nesting Channel Trees

You can build parts of your channel structure independently and nest them into a larger tree.

This is useful for large apps, feature modules, or organizing by domain.

// chat-channels.ts export const chatChannels = channels({ messages: [z.object({ sender: z.string(), text: z.string() })], typing: [z.object({ userId: z.string() })], }); // app-channels.ts export const appChannels = channels({ chat: chatChannels, notifications: [z.object({ title: z.string(), body: z.string() })], });

Result:

channels.chat.messages.emit(...); channels.chat.typing.emit(...); channels.notifications.emit(...);

Notes:

  • You can build isolated channel trees for different features.
  • Then nest them cleanly under static keys.
  • Type-safety and parameter awareness are preserved automatically.

Summary

  • Channel paths are dot-separated into a tree.
  • Static keys create nested objects.
  • Parameters are resolved dynamically using .for().
  • Deep and flexible structure improves clarity and scalability.
  • You can split large systems into smaller channel trees and nest them safely.
Last updated on