@leverage/plugin-websocket
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

@leverage/plugin-websocket

A Websocket plugin for Leverage

Install

# Using NPM
npm install @leverage/plugin-websocket

# Using Yarn
yarn add @leverage/plugin-websocket

Documentation

Plugin Install

To install the WebSocket Plugin, add websocket to Leverage.

import { add } from "@leverage/core";
import { websocket } from "@leverage/plugin-websocket";

add(websocket);

Events

The WebSocket Plugin can be configured using events. The server can also be told to attach to a HTTP plugin.

import { add, emit } from "@leverage/core";
import { websocket } from "@leverage/plugin-websocket";

add(http);

// Configure the Fastify instance.
await emit("websocket:configure", {
    /*
     * All configuration options are passed directly to socket.io.
     * https://socket.io/docs/v4/server-api/
     */
});

// Tell the server to attach to the HTTP plugin.
await signal("websocket:attach");

Components

A WebSocket component holds configuration for an event.

import { useWebSocket } from "@leverage/plugin-websocket";

export const init = () => {
    useWebSocket({
        event: "my-event",
    });
};

/*
 * A handler must be provided to handle the event. The handler
 *  receives the socket that triggered the event and any data
 *  from the event.
 */
export const handler = (socket, data) => {};

Hooks

useWebSocket

The useWebSocket hook can be used to configure a WebSocket component.

import { useWebSocket } from "@leverage/plugin-websocket";

export const init = () => {
    useWebSocket({
        event: "my-event",
    });
};

Package Sidebar

Install

npm i @leverage/plugin-websocket

Weekly Downloads

5

Version

2.1.1

License

Apache-2.0

Unpacked Size

8.3 kB

Total Files

6

Last publish

Collaborators

  • jakehamilton