Overview
Middleware handlers are registered for specific events and execute in priority order when that event is triggered. They are used for:Intercept Events
Run code when events fire
Log Activity
Log all event activity automatically
Collect Data
Gather data from multiple handlers
Side Effects
Trigger additional actions on events
Methods
Add
Register middleware for a specific event.string
required
Name of the event to intercept
function
required
Middleware function to execute.Parameters:
source(number) - Player who triggered event (if applicable)...- Additional event parameters
number
default:"1"
Execution priority (lower numbers execute first)
TriggerEvent
Trigger all middleware handlers for an event. All handlers run in priority order — errors in one handler do not stop others.string
required
Event name to trigger
number
required
Player source (or 0 if not player-related)
any
Additional parameters passed to all handlers
- Handlers are sorted by priority (lowest first)
- Each handler runs via
pcall— errors are caught and printed, never crash the server - All handlers run regardless — return values are ignored
- Errors print to console:
[Middleware] ERROR in 'eventName' handler (priority X): error message
TriggerEventWithData
Trigger middleware and collect return values from all handlers into a combined table. This is used when you need handlers to contribute data.string
required
Event name to trigger
number
required
Player source
any
Additional parameters
ID field assigned for ordering.
Handler return format:
Priority System
Lower priority numbers execute first:The default priority is
1 if not specified.Common Use Cases
Logging Player Actions
Tracking Connections
Economy Monitoring
Anti-Cheat Detection Logging
Collecting Data from Multiple Resources
Error Handling
Middleware handlers are wrapped inpcall. If a handler errors:
- The error is printed to console with the event name and priority
- Other handlers continue to execute — one error does not stop the chain
- For
TriggerEventWithData, errored handlers contribute nothing to the result
Resource Restart Behavior
Middleware handlers are cleared when any resource starts (except the current resource). This means:- Handlers are re-registered when resources restart
- Stale handlers from stopped resources are cleaned up automatically
Best Practices
Use Appropriate Priorities
Use Appropriate Priorities
Keep Handlers Lightweight
Keep Handlers Lightweight
Middleware runs on every event trigger. Keep handlers fast:
Use TriggerEventWithData for Data Collection
Use TriggerEventWithData for Data Collection
When you need handlers to contribute data, use For plain side-effects (logging, notifications), use
TriggerEventWithData:TriggerEvent instead.Middleware Cannot Block Events
Middleware Cannot Block Events
Unlike some frameworks, Mythic middleware does NOT support blocking:If you need to prevent an action, implement the check in the event handler itself, not in middleware.
Next Steps
Event System
Understanding the event system
Base API
Core framework exports
Logger API
Logging component
Callbacks
Request-response communication