PlayMesh/Docs
HomeGitHubnpm

Chat

Built-in chat with real-time server-side moderation.

Overview

Clients send chat with client.chat(text); the server broadcasts it to every instance the sender is a member of. No handlers or event names to wire up.
client.chat('hello everyone');
client.onChat(message => console.log(`${message.userId}: ${message.text}`));

Moderation

The server moderates every message in real time with the onChatMessage hook. Return true to pass a message through, a string to rewrite it, or false (or throw) to block it - only the sender is notified of a block.
mesh.onChatMessage(({ session, text }) => {
  if (containsSlurs(text)) {
    session.kick('Watch your language'); // escalate: see below
    return false;                        // and drop the message
  }
  return censor(text); // rewrite, or return true to pass through
});
Pair chat with the built-in rateLimit middleware to stop spam: mesh.use(rateLimit({ chat: 2 })).

Kicking sessions

session.kick(reason?) is a general session control, not a chat feature - it works anywhere you have a session (event handlers, middleware, admin tooling). The client is told why via client.onKick(reason => ...), then force-disconnected. Chat moderation is simply its most common trigger.
// Server - anywhere, not just chat hooks
session.kick('AFK too long');

// Client
client.onKick(reason => showKickedScreen(reason));