Middleware in stelar-time-real sits between the raw connection event and yourDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/foxytp/stelar-time-real/llms.txt
Use this file to discover all available pages before exploring further.
onConnection handler. Every incoming client passes through each registered middleware in the order it was added before being accepted onto the server. This gives you a single, predictable place to enforce authentication, logging, or any connection-level policy. If a middleware does not call next(), the pipeline stops and the connection is rejected — no onConnection handler fires, and no events are dispatched for that client.
Adding middleware
Register middleware withstelar.use(). Each function receives a context (ctx) and a next callback:
stelar.use() chain the middlewares sequentially — they run in the order they were registered.
Step-by-step: token authentication
Check the Authorization header
Read the token from Calling
ctx.req?.headers?.authorization. Because TCP clients do not perform an HTTP handshake, ctx.req will be null for them — use optional chaining to handle both protocols safely.ctx.ack('error', ...) sends an error frame back to the client before the connection is dropped because next() was never called.Store metadata on the client
Once the token is verified, attach any derived data (user ID, role, etc.) to the client context via
ctx.setMetadata(). This data travels with the client for the lifetime of the connection and is readable from any event handler via ctx.getMetadata().Example: IP block middleware
Usectx.socket.destroy() to immediately terminate a connection from a blocked IP without sending any response frame:
Example: logging middleware
Middleware is also useful for connection-level logging. Because it runs beforeonConnection, you capture every attempted connection — including ones that will be rejected by a later middleware:
TCP clients and ctx.req
ctx.req is the Node.js IncomingMessage object for WebSocket clients (it comes from the HTTP upgrade request). For clients connecting in TCP mode, no HTTP request is involved, so ctx.req is always null. Use optional chaining (ctx.req?.headers?.authorization) when writing middleware that should work for both protocols.