Skip to main content

PackedMessage

A memory-efficient compressed representation of a Message. Use PackedMessage when you need to retain many messages in memory (e.g., buffering, aggregation, or caching) and want to reduce memory overhead.

Create a PackedMessage by calling message.pack(). Unpack it back to a full Message when you need to access or modify the data.


At a Glance

// Pack a message for efficient storage
const packed = message.pack();

// Store in a buffer or cache
messageBuffer.push(packed);

// Later: unpack and process
const restored = packed.unpack();
stream.emit(restored, OUTPUT_PORT);

Properties

PropertyTypeDescription
typeDataDictionaryReference to the data dictionary used when packing
const packed = message.pack();
stream.logInfo(`Packed type: ${packed.type}`);

Methods

unpack()

Restores the packed message to a full Message instance.

Returns: Message

const packed = message.pack();
const restored = packed.unpack();

// restored is a full Message with all methods available
restored.getString(dataDictionary.type.Order.ID);
restored.addStatus(Severity.INFO, Status.create(VENDOR, 'RESTORED'));

When to Use

ScenarioApproach
Buffering messages for batch processingPack to reduce memory
Caching messages in a queue servicePack before storing
Aggregating many records before emittingPack intermediate results
Passing messages between processorsUse regular Message (no packing needed)

Complete Example

let buffer = [];
const BATCH_SIZE = 100;

export function onMessage() {
// Pack and buffer
buffer.push(message.pack());

// When buffer is full, process batch
if (buffer.length >= BATCH_SIZE) {
processBatch(buffer);
buffer = [];
}
}

export function onStreamEnd() {
// Process remaining messages
if (buffer.length > 0) {
processBatch(buffer);
}
}

function processBatch(packedMessages) {
for (const packed of packedMessages) {
const msg = packed.unpack();
// Process...
stream.emit(msg, OUTPUT_PORT);
}
}

See Also

  • Message#pack — Create a PackedMessage from a Message
  • Message#clone — Alternative: create a full copy instead of packing