Skip to main content

Chat module

@rpgjs/chat is an optional RPGJS v5 module with a server-authoritative message flow:
  1. The client submits text and a requested channel.
  2. The server normalizes and validates the text, applies rate limits and moderation, and reconstructs the author and timestamp.
  3. The server broadcasts the accepted ChatMessage.
  4. Each client stores the message in a reactive signal and renders it.
The default GUI is a CanvasEngine component, but presentation is replaceable without changing the server behavior.

Install

The default component also uses the semantic styles from @rpgjs/ui-css.

Configure the client

Use the explicit client entry point. This prevents server code from entering a browser bundle when standalone mode loads both runtime entry graphs.

Client options

What the built-in GUI does

  • Channel selector. With several channels, tabs above the messages choose where the next message goes. Each channel has a color (slate, amber, violet, emerald, rose, sky) shared by its tab and the left border of its messages. The placeholder names the selected channel.
  • Sending state. After a message is sent, the button shows a loading and the player cannot send another until the server delivers it or refuses it (an error shows for a few seconds). The wait ends by itself after 8 seconds.
  • Size and collapse. A button makes the box taller (--rpg-ui-chat-tall-height) with a short animation, which prefers-reduced-motion removes. Another one reduces the chat to a pill that counts the unread messages of the others.
  • Moderation tools. A message removed by a moderation tool disappears from every screen (chat:delete, below).
Everything is authoritative on the server: the GUI only reads what the server sends. In standalone mode the same code runs in the browser; in MMORPG mode the messages cross the connection as before. maxMessages limits client memory only. It does not create permanent history. The server remains authoritative for maxLength; configure the same value on both sides so the built-in input rejects exactly the same message lengths.

Configure the server

Use the explicit server entry point and register it before provideServerModules():
Map chat is enabled by default. A map message is broadcast only to players in the sender’s current map. An omitted channel defaults to map (or to the first channel when the game has no map chat); any value that is not one of the declared channels is rejected. See Channels to add more.

Server options and hooks

The hook order is:
A message refused at any step sends the player the error chat:error and calls onRejected.

Moderation example

Clients never choose the accepted author, player ID, map ID, or timestamp.

Channels

A channel is a place where players talk. The server owns who may write on it and who receives what is written: the client only asks for a channel id. map and global are built in. Declare any other channel with defineChatChannel():
The audience map is broadcast by the module. Any other audience is carried by deliver, which is responsible for reaching all intended clients, including clients hosted by another room, process, or edge location. A channel whose audience cannot be delivered (no deliver) refuses the message. Giving the same channels to the client shows the selector (see Client options).

Global chat

Global chat is opt-in because the correct delivery mechanism depends on the deployment topology. Enable the channel and provide deliver:
The legacy broadcastGlobal(message, player) option still works for the global channel; use deliver in new code.

Middleware

middleware is the place for moderation: mutes, filters, anti-spam, or an external classifier. Each step receives the message and next():
The steps run in order, around the delivery. A step that neither calls next() nor refuses refuses the message. beforeSend keeps working and runs first.

Client API

Replacement components and client modules can import: sendChatMessage() does not add an optimistic message. The UI changes only after the server broadcasts the accepted ChatMessage.

Message contract

Removing a message

A moderation tool removes a message by sending the event chat:delete (CHAT_DELETE_EVENT) with { id } to the clients that received it, for example map.broadcast(CHAT_DELETE_EVENT, { id }). Every client drops the message from its list. The module does not decide who may delete: that is the moderation of your game.

Replace the CanvasEngine component

A replacement component does not receive the message history as props. Instead, it consumes the public chat signals. GUI data contains the configured position and maxLength. Create gui/custom-chat.ce:
CanvasEngine’s DOMContainer binds value={draft} in both directions. submit={submitMessage} prevents the browser’s default form navigation and passes (event, formData) to the handler. Register the replacement:
See the CanvasEngine documentation for DOMContainer, template syntax, and reactive signals.

Use a Vue component

Install and configure the RPGJS Vue overlay as described in Vue.js integration, then use a Vue component as the replacement:
The Vue component can subscribe to chatMessages.observable and chatError.observable, and submit with sendChatMessage(). Remember to unsubscribe when the component unmounts.

Themes and CSS

The default component uses semantic rpg-ui-* classes. Import the base theme:
Add the pixel theme after it:
Then apply the theme class to an ancestor, usually <body>:
You can also override individual semantic classes:
The bar of the default component adds rpg-ui-chat-bar, rpg-ui-chat-tabs, rpg-ui-chat-tab (with data-color and aria-pressed), rpg-ui-chat-icon-button, rpg-ui-chat-pill and rpg-ui-chat-unread. The panel has data-size="default" | "tall"; set the tall height with --rpg-ui-chat-tall-height (34rem). Messages carry data-channel, data-color and data-self="true" for the player’s own messages. The channel hue comes from --rpg-ui-chat-channel-color.

Errors and translations

The default client module provides English and French texts for these keys: Override them through the regular RPGJS i18n configuration. A channel label is a key (or plain text), and so is the key a middleware gives to context.reject(): declare the texts of your game in the same way. Custom components should display chatError through engine.i18n().t() instead of exposing raw translation keys.

Run the pixel chat playground

The repository includes a complete standalone and MMORPG-compatible example:
Open http://localhost:5189. The example demonstrates a map and an Everyone channel with the selector, deliver, rate limiting, an afterSend automated reply, a CanvasEngine GUI, and the shared pixel theme. Private messages, voice chat, and permanent history are intentionally outside the core module.