0.1.4 • Published 1 year ago

@soketi/routing v0.1.4

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

@soketi/routing

@soketi/routing is a package that contains the routing logic for the soketipfs server, as well as the logic for handling custom events in the network.

Usage

Generic Router

The GenericRouter class is used to handle custom events in the network. It's a generic implementation that can be used to handle any type of events.

import { GenericRouter } from '@soketi/routing';

const router = new GenericRouter();

router.registerHandler('some-event', async (data) => {
  console.log(data);
});

router.handle('some-event', 'Hello world!');

The registerHandler and handle accept any number of arguments, which are passed to the handler.

router.registerHandler('add', async (a, b) => {
  return a + b;
});

const result = await router.handle('add', 1, 2);

Websockets Router

The WebsocketsRouter class is used to handle custom events in the network, but it's specifically designed to work with the WebSockets server by adding some specific events.

import { Connection } from '@soketi/connections';
import { WebsocketsRouter } from '@soketi/routing';

const router = new WebsocketsRouter();

// 1. New connection
router.onNewConnection(async (conn: Connection) => {
  // ...
});

await router.handleNewConnection(conn);

// 2. Connection closed
router.onConnectionClosed(async (conn: Connection) => {
  // ...
});

await router.handleConnectionClosed(conn);

// 3. Message handling
router.onMessage(async (conn: Connection, message: any) => {
  // ...
});

await router.handleMessage(conn, 'hello');

// 4. Error handling
router.onError(async (conn: Connection, error: any) => {
  // ...
});

await router.handleError(conn, 'hello');

// 5. Custom handlers
// Built on top of the GenericRouter, it can handle custom events.
router.registerHandler('some-event', async (data) => {
  console.log(data);
});

router.handle('some-event', 'Hello world!');

Development

This library was generated with Nx.

Building

Run nx build routing to build the library.

Running unit tests

Run nx test routing to execute the unit tests via Vitest.


0.1.4

1 year ago

0.1.3

1 year ago

2.0.0

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago