2.1.1 • Published 2 years ago

@leverage/plugin-websocket v2.1.1

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
2 years ago

@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",
    });
};
2.1.1

2 years ago

2.1.0

2 years ago

2.0.3

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.1

6 years ago