WsPlugin
This file defines the WsPlugin
class, which implements the WebSocket (WS) integration plugin for the @sodacore/core
framework. The plugin loads its metadata from package.json
and registers all necessary WebSocket services, middleware, and providers with the application, enabling real-time communication via WebSockets.
Features
- Automatic Metadata Loading: Reads plugin metadata (name, version, description, author) from
package.json
at runtime. - Service Registration: Registers the WebSocket connections provider, upgrade middleware, and WebSocket service with the application during installation.
- Dependency Management: Specifies a dependency on the
@sodacore/http
plugin to ensure HTTP services are available. - Extends BasePlugin: Inherits from
BasePlugin
for consistent plugin lifecycle and configuration handling.
Usage
typescript
import WsPlugin from './module/plugin';
import Application from '@sodacore/core';
const app = new Application();
const wsPlugin = new WsPlugin();
await wsPlugin.install(app);
API
Properties
- name: The plugin name, loaded from
package.json
. - version: The plugin version, loaded from
package.json
. - description: The plugin description, loaded from
package.json
. - author: The plugin author, loaded from
package.json
. - dependencies: An array of plugin dependencies (includes
@sodacore/http
).
Constructor
typescript
constructor(protected config: IConfig = {})
- config: Optional configuration object for the plugin.
Methods
async install(app: Application): Promise<void>
Registers the following with the Sodacore application instance:
WsConnections
(WebSocket connections provider)UpgradeMiddleware
(handles HTTP-to-WebSocket upgrades)WsService
(core WebSocket service)
How It Works
- Loads plugin metadata from
package.json
using Bun's file API. - Sets the plugin's metadata properties.
- On installation, registers all necessary WebSocket-related services and middleware with the Sodacore application.
Notes
- This plugin is designed to be used with the Sodacore framework and expects Bun as the runtime.
- Throws an error if
package.json
is not found. - All WebSocket-related functionality is encapsulated and registered via the
install
method. - Ensures HTTP support is available by declaring a dependency on
@sodacore/http
.