Chat module
@rpgjs/chat is an optional RPGJS v5 module with a server-authoritative
message flow:
- The client submits text and a requested channel.
- The server normalizes and validates the text, applies rate limits and moderation, and reconstructs the author and timestamp.
- The server broadcasts the accepted
ChatMessage. - Each client stores the message in a reactive signal and renders it.
Install
@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, whichprefers-reduced-motionremoves. 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).
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 beforeprovideServerModules():
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:
chat:error and calls
onRejected.
Moderation example
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 providedeliver:
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 eventchat: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 configuredposition and maxLength.
Create gui/custom-chat.ce:
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:
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:chatMessages.observable and
chatError.observable, and submit with sendChatMessage(). Remember to
unsubscribe when the component unmounts.
Themes and CSS
The default component uses semanticrpg-ui-* classes. Import the base theme:
<body>:
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: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.