Middleware & Plugins
Intercept events with middleware and extend PlayMesh with reusable plugins.
Middleware
Middleware functions intercept every incoming client event before it reaches instance handlers. They follow the
(context, next) pattern.Signature
Logging
Rate limiting (built in)
PlayMesh ships a
rateLimit middleware with per-session, per-event token buckets. '*' catches events without their own limit; over-limit events are rejected with an error the client sees via onError.Role-based access (built in)
requireRole protects events by role - a session needs at least one of the listed roles (granted by your authenticate hook). Events without a rule stay open. Denied events reach the client via onError.Custom authorization
⚠
Calling
next()multiple times in the same middleware throws a "next() called multiple times" error.Plugins
Plugins are objects with an
install(mesh) method. They receive the PlayMesh instance and can register hooks, middleware, domains, and anything else.Plugin interface
Writing a plugin
Using a plugin
✦
Plugins are installed before bootstrap hooks run, so a plugin can safely register its own bootstrap hooks.