1. Install and import
npm install js-messageimport Message from 'js-message';This installs js-message 3.0.0 from npm. The package is a native ES module with no runtime dependency or compile step.
2. Create and serialize
const message = new Message();
message.type = 'order.created';
message.data = {
id: 'ord_481',
total: 27.50
};
transport.send(message.JSON);message.JSON is a string containing only type and data. Other properties you add to the instance stay local.
Already have an envelope?
Pass a JSON string or an object directly to the constructor: new Message(input).
3. Load and branch
const incoming = new Message();
incoming.load(receivedValue);
if (incoming.type === 'error') {
console.error(incoming.data.err);
} else {
route(incoming.type, incoming.data);
}load() accepts a JSON string or an existing object. A malformed string or invalid envelope becomes a recoverable type: 'error' message instead of escaping from load().