Message

A structured WebSocket message object for typed payload exchange over WebConnect.

Import
the javascript logo
import { Message } from "@schukai/monster/source/net/webconnect/message.mjs";
Source
the git logo
Package
the npm logo
Since
3.4.0

Message is the transport wrapper used around WebConnect payloads. It keeps the message body in one explicit object that can be serialized to JSON, queued, logged and reconstructed later.

Use it when the realtime boundary needs a stable shape. A dedicated message object is easier to inspect in tests, easier to persist in queues and safer to hand to tooling than a loose collection of plain objects.

The class is deliberately minimal: it stores the payload, exposes it through getData() and toJSON(), and restores it through fromJSON(). Application semantics stay in the payload itself.

Serialize And Restore A Structured Message

import { Message } from "@schukai/monster/source/net/webconnect/message.mjs";

const output = document.getElementById("message-output");

document.getElementById("message-run").addEventListener("click", () => {
    const source = new Message({
        type: "notification",
        payload: {
            id: "evt-1",
            severity: "info",
        },
    });

    const json = JSON.stringify(source);
    const restored = Message.fromJSON(json);

    output.textContent = JSON.stringify(
        {
            serialized: json,
            restored: restored.getData(),
        },
        null,
        2,
    );
});
Open in playground

Keep Nested Payload Data Intact Through Serialization

import { Message } from "@schukai/monster/source/net/webconnect/message.mjs";

const output = document.getElementById("message-nested-output");

document.getElementById("message-nested-run").addEventListener("click", () => {
    const source = new Message({
        type: "presence",
        payload: {
            users: [
                { id: "u-1", status: "online" },
                { id: "u-2", status: "away" },
            ],
            meta: {
                room: "alpha",
                revision: 3,
            },
        },
    });

    const restored = Message.fromJSON(JSON.stringify(source));
    output.textContent = JSON.stringify(restored.getData(), null, 2);
});
Open in playground

Use A Message Shape That Is Ready For Queue Inspection And Logging

import { Message } from "@schukai/monster/source/net/webconnect/message.mjs";

const output = document.getElementById("message-queue-output");

document.getElementById("message-queue-run").addEventListener("click", () => {
    const queue = [
        new Message({ type: "joined", payload: { room: "alpha", user: "u-1" } }),
        new Message({ type: "typing", payload: { room: "alpha", user: "u-2" } }),
    ];

    output.textContent = JSON.stringify(
        queue.map((entry) => entry.toJSON()),
        null,
        2,
    );
});
Open in playground

Access Raw Message Data Through GetData And ToJSON

import { Message } from "@schukai/monster/source/net/webconnect/message.mjs";

const output = document.getElementById("message-object-output");

document.getElementById("message-object-run").addEventListener("click", () => {
    const message = new Message({
        type: "notification",
        payload: {
            severity: "warn",
            id: "evt-8",
        },
    });

    output.textContent = JSON.stringify(
        {
            fromGetData: message.getData(),
            fromToJSON: message.toJSON(),
        },
        null,
        2,
    );
});
Open in playground

Exported

Message

Derived from

Base

Options

The Options listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the Base.

Option
Type
Default
Description
-/-

Properties

The Properties listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Properties from these parent classes. If you cannot find a specific Properties in this list, we recommend consulting the documentation of the Base.

Methods

The methods listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the Base.

Constructor

constructor(data)
Parameters
  • data {object}: data
Throws
  • {TypeError} value is not a object

Structural methods

getData()
Returns
  • {object}
Returns the raw message.

Static methods

fromJSON(json)
Parameters
  • json {string}: json
Returns
  • {Message}
Throws
  • {TypeError} value is not a string

Other methods

toJSON()
Returns
  • {*}

Events

This component does not fire any public events. It may fire events that are inherited from its parent classes.

The current width of the area is too small to display the content correctly.