Skip to content

Room Plugin Example: Writing the TypingList Plugin

Benjamin Raymond edited this page Nov 26, 2023 · 3 revisions

Room Plugin Example: Writing the TypingList Plugin

Overview

This guide provides a step-by-step process to develop the TypingListPlugin, a room-specific plugin for the SkyChat application. This plugin tracks and manages the list of users currently typing in a chat room.

Step-by-Step Guide

Step 1: Plugin Setup

  • Create TypingListPlugin Class:
    • Extend the TypingListPlugin class from RoomPlugin.

    • Define the command name as t.

      export class TypingListPlugin extends RoomPlugin {
          static readonly commandName = 't';
          // ...
      }

Step 2: Define Command Rules

  • Set Command Rules:
    • Define minimum and maximum parameter counts.

    • Specify a pattern for the action parameter (on, off, clear).

      readonly rules = {
          // Usage:
          //   /t on
          //   /t off
          //   /t clear (OP only)
          t: {
              minCount: 1,
              maxCount: 1,
              params: [{ name: 'action', pattern: /^(on|off|clear)$/ }],
          },
      };

Step 3: Implement the Run Method

  • Handle Different Actions:
    • Clear the typing list if the clear command is used (with OP rights check).

    • Register a user as typing with the on command.

    • Remove a user from the typing list with the off command.

      async run(alias: string, param: string, connection: Connection): Promise<void> {
          // Implement action handling logic here...
      }

Step 4: Manage Typing List

  • Maintain Typing List:
    • Use a private variable typingList to track users and their typing start time.

      private typingList: { [identifier: string]: { startedDate: Date; user: User } } = {};

Step 5: Synchronize Typing List

  • Implement Sync Method:
    • Create a private method sync to update the room with the current typing list.

      private sync(): void {
          // Synchronization logic here...
      }

Step 6: Handle Room Events

  • Use Hooks:
    • Implement onConnectionJoinedRoom to sync the typing list when a new connection joins the room.

    • Implement onConnectionLeftRoom to remove a user from the typing list when they leave the room.

      async onConnectionJoinedRoom(): Promise<void> {
          this.sync();
      }
      
      async onConnectionLeftRoom(connection: Connection): Promise<void> {
          delete this.typingList[connection.session.identifier];
      }

Testing and Debugging

  • Ensure the plugin behaves as expected in different scenarios:
    • When users start and stop typing.
    • When users join or leave the room.
    • When the typing list is cleared by an operator.

See the full source code of the TypingList plugin