Customize the Plugin
The SDK automatically registers @4players/odin-plugin-web as its default audio plugin when needed -- no manual initialization is required for most use cases. This page explains how to customize the plugin when the defaults are not sufficient, and documents the Plugin API interface for advanced use cases.
Registering a custom plugin
If you want to customize the plugin (e.g. to enable logging or disable auto-reconnect), create one manually using createPlugin() from @4players/odin-plugin-web and register it with ensurePlugin() from @4players/odin:
If you call ensurePlugin() with a custom plugin, do so before any other SDK call (e.g. before setOutputDevice() or room.join()). If no custom plugin is registered, the SDK will create and use a default one automatically.
Replacing the active plugin
If a plugin is already registered and you need to swap it out (e.g. during testing or to change configuration at runtime), use replacePlugin():
Unlike ensurePlugin, this always overwrites the current plugin, even if one is already active.
createPlugin parameters
The createPlugin function from @4players/odin-plugin-web accepts two arguments:
audioContextFactory
A factory function the SDK calls whenever it needs an AudioContext. It receives the desired sample rate and must return a Promise<AudioContext>:
The default plugin creates this factory for you automatically. When providing your own, make sure to call audioContext.resume() inside the factory, as browsers require this to start audio processing.
options (WebPluginOptions)
An optional configuration object with the following properties:
| Property | Type | Default | Description |
|---|---|---|---|
onLogMessage | (formattedString: string, record: LogRecord) => void | undefined | Callback invoked for every internal log message. The formattedString is a pre-formatted human-readable string; record contains structured data (level, timestamp, logger name, message). |
logger | Logger | Built-in logger | A custom Logger instance from @4players/odin-common. When provided, it replaces the built-in logger entirely. Most use cases should use onLogMessage instead. |
disableAutoReconnect | boolean | false | When true, the plugin will not automatically attempt to reconnect after a connection loss. Useful if your application implements its own reconnection logic. |
disableBeforeUnload | boolean | false | When true, the plugin will not register a beforeunload handler on the window object. By default, the plugin auto-closes all connections when the page is being unloaded. |
Logging example
Disabling auto-reconnect
Plugin API
The SDK defines a Plugin interface that any plugin must implement. The built-in web plugin (@4players/odin-plugin-web) implements this interface, but you can also build a custom plugin as long as it conforms to the same contract.
Key types
Implementing a custom plugin
A custom plugin must set version to '8' (the current SDK API version) and implement all methods of the interface. The SDK validates the version when the plugin is registered and will throw an error if it does not match.
A native Node.js plugin for server-side and non-browser environments is currently in development. This will allow running ODIN audio processing natively without a browser AudioContext.