0.0.6 • Published 4 months ago

@tsdiapi/socket.io v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

TSDIAPI Socket.IO Plugin for TSDIAPI-Server

The @tsdiapi/socket.io plugin integrates WebSocket functionality into the TSDIAPI-Server, utilizing Socket.IO and socket-controllers. This plugin offers type-safe event handling with TypeScript decorators and supports both authenticated and unauthenticated connections.


Installation

Install the plugin via NPM:

npm install @tsdiapi/socket.io socket-controllers

Or use the CLI to install it in your project:

tsdiapi plugins add socket.io

Quick Start

Add the Plugin

Register the plugin in your TSDIAPI-Server application:

import { createApp } from "@tsdiapi/server";
import ioPlugin from "@tsdiapi/socket.io";

createApp({
  plugins: [ioPlugin()],
});

Code Generation

NameDescription
baseGenerates a new Socket.IO event handler.

The TSDIAPI-Socket.IO plugin includes a generator to create socket event handlers easily. Use the tsdiapi CLI to generate a new event controller:

tsdiapi generate socket.io

You will be prompted to enter an event name from the @base/sockets.types file, which is generated when the plugin is added.

Define a WebSocket Controller

Create a controller to handle socket events:

import {
  ConnectedSocket,
  MessageBody,
  OnConnect,
  OnDisconnect,
  OnMessage,
  SocketController,
} from "socket-controllers";
import { AuthAppSocket } from "@tsdiapi/socket.io";
import { Service } from "typedi";

export enum SocketEvent {
  helloWorld = "helloWorld",
}

export interface SocketPayloads {
  [SocketEvent.helloWorld]: {};
}

export interface SocketResponses {
  [SocketEvent.helloWorld]: {
    message: string;
  };
}

type AppSocketType = AuthAppSocket<
  { userId: string },
  SocketEvent,
  SocketEvent,
  SocketPayloads,
  SocketResponses
>;

@SocketController()
@Service()
export class MessageController {
  @OnConnect()
  connection(@ConnectedSocket() socket: AppSocketType) {
    console.log("Client connected");
  }

  @OnDisconnect()
  disconnect(@ConnectedSocket() socket: AppSocketType) {
    console.log("Client disconnected");
  }

  @OnMessage(SocketEvent.helloWorld)
  async onHelloWorld(
    @ConnectedSocket() socket: AppSocketType,
    @MessageBody() message: SocketPayloads[SocketEvent.helloWorld]
  ) {
    console.log("Received message:", message);
    socket.emitSuccess(SocketEvent.helloWorld, {
      message: "Hello World!",
    });
  }
}

Key Features

  • Declarative Controllers: Handle events like connect, disconnect, and messages with decorators such as @OnConnect and @OnMessage.
  • Authentication Support: Use AuthAppSocket to secure and manage authenticated socket connections.
  • Type Safety: Define strongly typed payloads and responses for each event.
  • Seamless Integration: Automatically register and manage WebSocket controllers within the TSDIAPI-Server lifecycle.

Plugin Lifecycle Integration

The plugin hooks into the TSDIAPI lifecycle:

  • onInit: Initializes the WebSocket server.
  • afterStart: Starts listening for WebSocket connections and events.
  • beforeStart: Allows custom pre-start setup if needed.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to improve the plugin.


License

This library is licensed under the MIT License. See the LICENSE file for details.

0.0.1

4 months ago

0.0.3

4 months ago

0.0.2

4 months ago

0.0.5

4 months ago

0.0.4

4 months ago

0.0.6

4 months ago

0.0.1-alpha.4

4 months ago

0.0.1-alpha.3

5 months ago

0.0.1-alpha.2

5 months ago

0.0.1-alpha.1

5 months ago