Zodiac API Documentation - v0.0.3
    Preparing search index...

    Class NodeChannel

    Basic server usage:

    import { NodeChannel, defineMessage, z } from '@xtr-dev/zodiac';

    const channel = new NodeChannel();
    await channel.connect('ws://management-server:8080');

    Microservice communication:

    const channel = new NodeChannel();
    channel.setReconnectOptions(50, 1000); // Very resilient
    await channel.connect('ws://user-service:3001/events');

    Hierarchy

    • BaseChannel
      • NodeChannel
    Index

    Constructors

    Methods

    • Sends a type-safe, validated message using a message definition.

      This is the preferred way to send messages as it provides:

      • Compile-time type checking
      • Runtime validation with Zod
      • Automatic message ID handling

      Type Parameters

      Parameters

      • definition: T

        Message definition created with defineMessage()

      • data: InferMessageData<T>

        Message data that matches the definition's schema

      Returns Promise<void>

      When data doesn't match the schema

      const userMsg = defineMessage('user', z.object({
      name: z.string(),
      age: z.number().min(0)
      }));

      // Type-safe and validated
      await channel.sendMessage(userMsg, {
      name: 'Alice',
      age: 25
      });

      // TypeScript error: missing required field
      await channel.sendMessage(userMsg, { name: 'Bob' }); // Error!

      // Runtime error: validation fails
      await channel.sendMessage(userMsg, { name: 'Charlie', age: -1 }); // Throws!
    • Registers a message handler for a specific message type (by string ID).

      Type Parameters

      • T

      Parameters

      • id: string

        String identifier for the message type

      • handler: MessageHandler<T>

        Function to handle incoming messages

      Returns void

    • Registers a type-safe message handler using a message definition. This is the preferred method as it provides full type safety.

      Type Parameters

      Parameters

      Returns void

      const chatMsg = defineMessage('chat', z.object({
      user: z.string(),
      text: z.string()
      }));

      // Fully typed handler - data.user and data.text are known
      channel.on(chatMsg, (data, message) => {
      console.log(`${data.user}: ${data.text}`);
      console.log(`Sent at: ${message.timestamp}`);
      });
    • Removes a message handler for a specific message type (by string ID).

      Type Parameters

      • T

      Parameters

      • id: string

        String identifier for the message type

      • handler: MessageHandler<T>

        The exact handler function to remove

      Returns void

    • Removes a type-safe message handler using a message definition.

      Type Parameters

      Parameters

      Returns void

      const handler = (data) => console.log(data.text);

      // Register handler
      channel.on(chatMsg, handler);

      // Remove the same handler instance
      channel.off(chatMsg, handler);
    • Checks if the channel is currently connected and ready for communication.

      Returns boolean

      true if connected, false otherwise

      if (channel.isOpen()) {
      await channel.sendMessage(someMessage, data);
      } else {
      console.log('Channel is not connected');
      }
    • Establishes a WebSocket connection using the Node.js 'ws' library.

      Optimized for server environments with enhanced error reporting and automatic reconnection handling.

      Parameters

      • url: string

        WebSocket server URL (ws:// or wss://)

      Returns Promise<void>

      If already connected or connection is in progress

      If WebSocket creation fails

      // Connect to local development server
      await channel.connect('ws://localhost:8080');

      // Connect to internal microservice
      await channel.connect('ws://user-service:3001/websocket');

      // Connect with authentication in URL
      await channel.connect('wss://api.example.com/ws?token=abc123');
    • Closes the connection and cleans up resources.

      Returns Promise<void>

    • Sends raw data over the channel. Use sendMessage() for type-safe validated sending instead.

      Type Parameters

      • T

      Parameters

      • id: string

        Message type identifier

      • data: T

        Raw message data

      Returns Promise<void>

    • Configures automatic reconnection behavior for Node.js environments.

      Particularly useful for microservices and server applications that need high reliability and resilience to network issues.

      Parameters

      • maxAttempts: number

        Maximum number of reconnection attempts (default: 5)

      • baseDelay: number

        Initial delay in milliseconds (default: 1000)

      Returns void

      // High-reliability microservice configuration
      channel.setReconnectOptions(100, 500);

      // Development environment (quick failure)
      channel.setReconnectOptions(3, 1000);

      // Critical service (very persistent)
      channel.setReconnectOptions(1000, 2000);