How to write a YAKjs plugin

Example of an empty plugin.

'use strict';

/* eslint-disable no-empty-function, no-unused-vars */

/**
 * @constructor
 * @struct
 * @param {!PluginContext} context
 */
function EmptyPlugin(context) {
    this.onStart = () => {};

    /**
     * @param {!WebSocketConnection} connection
     */
    this.onNewConnection = connection => {};

    /**
     * @param {!WebSocketMessage} message
     * @param {!WebSocketConnection} connection
     */
    this.onMessage = (message, connection) => {};

    /**
     * @param {!WebSocketMessage} message
     * @param {!WebSocketConnection} connection
     */
    this.onJsonMessage = (message, connection) => {};

    /**
     * @param {!WebSocketConnection} connection
     */
    this.onConnectionClosed = connection => {};

    this.onStop = () => {};
}

module.exports = {
    name: 'empty',
    description: 'This plugin does nothing.',
    createWorker: context => new EmptyPlugin(context)
};

The plugin object

ParameterTypeDescription
name

string

The unique name of the plugin. Prefer a URL safe name.

description

string

An optional information about the plugin. Will be displayed on the user interface.

createWorker

function(context:PluginContext):PluginWorker

The returned plugin worker will run in the context of one instance.

PluginWorker

PropertyTypeDescription
onStart

function()

Optional. Called when the instance started.

onNewConnection

function(connection:WebSocketConnection)

Optional. Called whenever a client has an WebSocket connection established.

onMessage

function(message:WebSocketMessage)

Optional. Called when a client has send data over the WebSocket connection to the instance.

onJsonMessage

function(message:WebSocketMessage)

Optional. Like onMessage, but the WebSocketMessage provides an object (deserialized) instead of a string.

onConnectionClosed

function(connection:WebSocketConnection)

Optional. Called when a client closed the connection.

onStop

function()

Optional. Called when the instance stopped.

WebSocketMessage

A received message.

PropertyTypeDescription
data

string | object

The message data. onJsonMessage provides an object when data was successfully parsed.

WebSocketConnection

PropertyTypeDescription
send

function(message:string)

Sends a message on that connection.

PluginContext

PropertyTypeDescription
instance

WebSocketInstance

The WebSocket server instance that uses this plugin.

log

Logger

A logger running in the context of this plugin.

WebSocketInstance

PropertyTypeDescription
id

string

The WebSocket server instance ID.

name

string

The WebSocket server instance name.

port

number

The WebSocket server instance port.

plugins

Array<string>

The list of assigned plugins.

state

string

The WebSocket server instance state (starting, stopped, running, stopping, error).

Logger

PropertyTypeDescription
info

function(message, [data])

Logs the message with optional data as INFO to the plugin log file.

warn

function(message, [data])

Logs the message with optional data as WARN to the plugin log file.

error

function(message, [data])

Logs the message with optional data as ERROR to the plugin log file.

debug

function(message, [data])

Logs the message with optional data as DEBUG to the plugin log file.