Overview
Thepublish endpoint is a protected mutation procedure that publishes a chat message to a specified channel. It includes content validation, rate limiting, and support for reply threads.
Procedure Type
Mutation - Protected procedure (authentication required)Authentication
This endpoint requires a valid JWT token. The authenticated user’s information (fromctx.user) is automatically attached to the published message.
Input Parameters
The message content to publish. Will be trimmed before validation.
The channel identifier to publish the message to
Optional message ID to reply to. Use
null or omit for top-level messages.Response Structure
Unique identifier for the message (UUID v4)
The trimmed message content that was published
Message ID this message is replying to, or
null if not a replyValidation Rules
Empty Message Check
After trimming whitespace, the content must not be empty. Error:BAD_REQUEST - “Message cannot be empty.”
Message Length Validation
The message content length must not exceed themaxMessageLength limit for the channel.
Error: BAD_REQUEST - “Message exceeds characters.”
Rate Limiting
Rate limiting is enforced per user (byrobloxUserId) per channel. The limits are retrieved using getChatLimitsForChannel(input.channel):
- Limit Count:
rateLimitCountmessages - Window:
rateLimitWindowMsmilliseconds
TOO_MANY_REQUESTS - “Rate limit hit. Try again in s.”
The error message includes the retry-after time in seconds.
Message Broadcasting
Successfully published messages are broadcast to all subscribers on the channel using the global pub/sub system (globalPubSub.emit).
Example Usage
Example Response
Error Handling
Implementation Details
The publish endpoint is implemented atchat.ts:19-68 and uses:
getChatLimitsForChannelfor retrieving channel limits (line 28)ratelimitservice for rate limit enforcement (lines 45-50)crypto.randomUUID()for message ID generation (line 59)globalPubSub.emitfor broadcasting messages (line 65)