XRS Reactive Server

1.2.2 · active · verified Tue Apr 21

xrs is a JavaScript library designed for building reactive servers and clients, currently at version 1.2.2. It facilitates full-duplex communication by treating both client requests and server responses as streams, promises, or plain values. The server component integrates the Express framework for routing and μWS (uws) for high-performance WebSocket handling, enabling efficient management of both HTTP and WebSocket connections. A key differentiator is its emphasis on stream-based interactions, allowing complex real-time data flows and supporting features like binary uploads with progress event tracking. The client-side library is designed to be lightweight, bundling at approximately 3 KB. While specific release cadence information is not provided, the current version suggests it is either actively maintained or stable. It's suitable for applications requiring low-latency, real-time communication with built-in stream processing capabilities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up an XRS server on a specific port, connecting an XRS client, sending various data types (string, binary, promise), and handling server responses and progress events.

import xrs from 'xrs';
import http from 'http'; // For server closing

async function runXRSExample() {
  const serverPort = 4000; // Choose a specific port

  // 1. Start the XRS Server
  // The 'xrs' function can take a processor or an options object.
  // We pass options to specify the port.
  const xrsServer = xrs({
    port: serverPort,
    processor: (req) => {
      console.log(`[Server] Received data from client (id: ${req.id}):`, req.data.toString());
      if (req.data === 'trigger-error') {
        throw new Error('Simulated server error');
      }
      return `Echo: ${req.data.toString()}`; // Send back a response
    }
  });

  // Access the underlying HTTP server instance to confirm it's listening
  // and for clean shutdown.
  await new Promise<void>(resolve => {
    xrsServer.http.on('listening', () => {
      console.log(`[Server] XRS server running on ws://localhost:${serverPort}`);
      resolve();
    });
  });

  // 2. Initialize the XRS Client
  // Pass the server URL to connect.
  const send = xrs(`ws://localhost:${serverPort}`);

  // 3. Client sends a simple value
  console.log('[Client] Sending "Hello Reactive Server!"');
  const clientResponseStream1 = await send('Hello Reactive Server!');
  clientResponseStream1.on('data', (data) => {
    console.log('[Client] Received response:', data);
  });
  clientResponseStream1.on('error', (err) => {
    console.error('[Client] Error on stream 1:', err);
  });

  // 4. Client sends a binary stream with progress events
  const binaryBuffer = Buffer.from('This is a test binary payload for upload.');
  console.log('[Client] Sending binary data...');
  const results: any[] = [];
  const clientResponseStream2 = await send(binaryBuffer, { metadata: 'binary-upload' });
  clientResponseStream2
    .on('sent', d => results.push({ type: 'sent', value: d }))
    .on('progress', d => results.push({ type: 'progress', value: d }))
    .on('data', d => results.push({ type: 'complete', value: d }));

  clientResponseStream2.on('end', () => {
    console.log('[Client] Binary upload events:', results);
  });

  // 5. Client sends a promise that resolves
  console.log('[Client] Sending a Promise that resolves...');
  const promiseResponseStream = await send(Promise.resolve('Data from Promise'));
  promiseResponseStream.on('data', (data) => {
    console.log('[Client] Received response for Promise:', data);
  });

  // Wait a bit, then close the server
  setTimeout(() => {
    console.log('[Server] Shutting down XRS server.');
    xrsServer.http.close(() => {
      console.log('[Server] XRS server closed.');
    });
  }, 3000);
}

runXRSExample().catch(console.error);

view raw JSON →