Skip to main content
Gate provides a powerful event system that allows you to listen to and modify events that occur in the proxy. Events are the primary way to communicate between your code and Gate, making it easy to extend the proxy’s functionality without modifying its core.

Event Architecture

Gate’s event system is built on the github.com/robinbraemer/event package, providing a type-safe, high-performance event bus for Go applications.

Key Components

Event Manager

The event.Manager is the central component that handles event subscriptions and dispatching. You can access it from the proxy instance:
The event manager is responsible for:
  • Managing event subscriptions
  • Dispatching events to registered handlers
  • Handling event priorities
  • Managing the event lifecycle

Event Types

Gate defines numerous event types in pkg/edition/java/proxy/events.go. Each event is a Go struct that contains relevant data and methods for that specific event. Common event categories include:
  • Connection Events: ConnectionEvent, ConnectionHandshakeEvent
  • Authentication Events: PreLoginEvent, LoginEvent, PostLoginEvent
  • Player Events: DisconnectEvent, PlayerChatEvent, PlayerSettingsChangedEvent
  • Server Events: ServerPreConnectEvent, ServerConnectedEvent, ServerPostConnectEvent
  • Lifecycle Events: ReadyEvent, PreShutdownEvent, ShutdownEvent
  • Plugin Message Events: PluginMessageEvent, PlayerChannelRegisterEvent

Subscribing to Events

To handle an event, you subscribe a handler function to the event manager using event.Subscribe:

Subscription Syntax

The event.Subscribe function has the following signature:
  • manager: The event manager instance (from proxy.Event())
  • priority: Handler execution order (lower values execute first)
  • handler: Your event handler function that receives the event

Event Priorities

Event handlers are executed in order of their priority, from lowest to highest. This allows you to control the execution order when multiple handlers are registered for the same event.
Common priority conventions:
  • Negative priorities (-100, -50): Early/pre-processing handlers
  • Priority 0: Normal/default handlers
  • Positive priorities (50, 100): Late/post-processing handlers

Event Cancellation

Many events support cancellation or modification of their behavior. Events typically provide methods to allow, deny, or modify the default action:

Modifying Events

Some events allow you to modify data before it’s processed:

Fire Events

While most events are fired automatically by Gate, you can also fire custom events or trigger existing event types:
The Fire method:
  • Executes all registered handlers in priority order
  • Blocks until all handlers complete
  • Passes the event to each handler

Event Handling Best Practices

1. Keep Handlers Fast

Event handlers should execute quickly, especially for high-frequency events like PingEvent:

2. Handle Errors Gracefully

3. Use Type Safety

Gate’s event system is fully type-safe. You always receive the correct event type:

4. Check for Nil Values

Some event fields may be nil depending on the context:

Next Steps