Plugin Architecture
Lavalink’s plugin system is built on Spring Boot’s dependency injection framework, allowing plugins to seamlessly integrate with the core server functionality.Architecture Overview
Plugins are loaded as JAR files and integrated into Lavalink’s Spring application context. This allows plugins to:- Register beans that implement plugin interfaces
- Access Lavalink’s services and components
- Define custom configuration properties
- Create REST endpoints and WebSocket handlers
- Extend audio processing capabilities
Plugin API Interfaces
The plugin API provides several interfaces for extending Lavalink. All interfaces are in thedev.arbjerg.lavalink.api package.
Core Extension Points
AudioFilterExtension
AudioFilterExtension
Define custom audio filters that can be controlled via WebSocket operations.Interface:Use Cases:
- Custom equalizers
- Audio effects (reverb, echo, etc.)
- Volume normalization
- Channel mixing
@Service annotation to your implementation.AudioPluginInfoModifier
AudioPluginInfoModifier
Add custom fields to track and playlist JSON responses.Interface:Use Cases:
- Add lyrics or metadata
- Include sponsorship information
- Attach additional track details
- Custom playlist metadata
JsonObject with custom fields, or null to skip modification.RestInterceptor
RestInterceptor
Intercept and modify HTTP requests to Lavalink’s REST API.Interface:This extends Spring’s
HandlerInterceptor, providing methods:preHandle()- Before request processingpostHandle()- After request processingafterCompletion()- After view rendering
- Custom authentication
- Request logging
- Rate limiting
- Request validation
@Service annotation to your implementation.PluginEventHandler
PluginEventHandler
Handle WebSocket lifecycle and player events.Interface:Events:
onWebSocketOpen- New WebSocket connection (or resumed)onSocketContextPaused- WebSocket closed but resumableonSocketContextDestroyed- WebSocket permanently closedonWebSocketMessageOut- Outgoing WebSocket messageonNewPlayer- Player created for a guildonDestroyPlayer- Player destroyed
- Analytics and monitoring
- Custom logging
- State synchronization
- Third-party integrations
@Service or @Component.AudioPlayerManagerConfiguration
AudioPlayerManagerConfiguration
Configure Lavalink’s Use Cases:
AudioPlayerManager instance.Interface:- Modify global player settings
- Register custom audio source managers
- Configure frame buffer duration
- Set up custom audio filters
AudioSourceManager beans, you don’t need this interface - just provide them as Spring beans directly.Lavaplayer Integration
Plugins can extend Lavaplayer’s audio processing capabilities by providing beans:API Objects
The plugin API provides access to Lavalink’s core objects:IPlayer
Represents an audio player for a specific guild.audioPlayer- The underlying LavaplayerAudioPlayertrack- Currently playing track (null if none)guildId- Discord guild ID (immutable)socketContext- Associated WebSocket connectionisPlaying- Whether actively producing audio
ISocketContext
Represents a WebSocket connection from a client.sessionId- Unique session identifieruserId- Discord user ID of the clientclientName- Optional client nameplayers- All players for this connection (by guild ID)state- Current connection state
ISocketServer
Provides access to all WebSocket sessions.Plugin Lifecycle
Loading
Dependency Resolution
If configured in
application.yml, Lavalink downloads plugins from Maven repositories.Runtime
Plugins interact with Lavalink through:- Event callbacks -
PluginEventHandlermethods called by Lavalink - Bean injection - Access to
ISocketServerand other Lavalink services - REST endpoints - Handle HTTP requests via Spring controllers
- Filter pipeline - Process audio through
AudioFilterExtension - Interceptors - Modify requests/responses via
RestInterceptor
Shutdown
When Lavalink stops:- Spring calls
@PreDestroymethods on plugin beans - Audio source managers’
shutdown()methods are called - Plugin resources are cleaned up
- ClassLoaders are released
Dependency Injection
Plugins can inject Lavalink services and other beans:Thread Safety
When implementing plugins, consider thread safety: Best Practices:- Use thread-safe collections (e.g.,
ConcurrentHashMap) - Synchronize access to shared mutable state
- Prefer immutable data structures
- Use
@Scope("prototype")for stateful beans
Error Handling
Plugins should handle errors gracefully to avoid crashing Lavalink:Testing
The Lavalink Gradle plugin provides tasks for testing:- Test WebSocket event handling
- Verify REST endpoints work correctly
- Check audio filters produce expected output
- Test configuration loading
- Verify error handling
- Test with multiple concurrent clients
Resources
Plugin API Javadoc
Complete API reference
Spring Boot Docs
Spring Framework documentation
Lavaplayer Docs
Audio processing library
Plugin Examples
Official and community plugins
Next Steps
Plugin Development Guide
Step-by-step plugin creation
Available Plugins
Explore existing plugins