Protobuf

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data, Initially released on July 7, 2008. It is useful in developing programs to communicate with each other over a network or for storing data. The method involves an interface description language that describes the structure of some data and a program that generates source code from that description for generating or parsing a stream of bytes that represents the structured data.

Supported versions

  • proto2

  • proto3

Supported Features

  • Retrieve compiled protobuf schemas (Produce messages without .proto files)

  • Versioning

  • Embedded serialization

  • Live evolution

  • Import packages (soon)

  • Import types (soon)

Getting started

How to produce a message

Memphis abstracts the need for external serialization functions and embeds them within the SDK.

In node.js, we can simply produce an object. Behind the scenes, the object will be serialized based on the attached schema and data format - protobuf.

Example schema: (No need to compile)

syntax = "proto3";
message Test {
            string field1 = 1;
            string  field2 = 2;
            int32  field3 = 3;
}

Producing a message without a local .proto file:

const { memphis } = require("memphis-dev");

(async function () {
    let memphisConnection

    try {
        memphisConnection = await memphis.connect({
            host: "MEMPHIS_BROKER_URL",
            username: "APPLICATION_USER",
            password: "PASSWORD",
            accountId: ACCOUNT_ID //*optional* In case you are using Memphis.dev cloud
        });
        const producer = await memphisConnection.producer({
            stationName: "STATION_NAME",
            producerName: "PRODUCER_NAME"
        });
        var payload = {
            field1: "AwesomeString",
            field2: "AwesomeString",
            field3: 54
        };
        await producer.produce({
            message: payload
        })
        memphisConnection.close();

    } catch (ex) {
        console.log(ex);
        if (memphisConnection) memphisConnection.close();
    }
})();

How to consume a message (Deserialization)

const { memphis } = require("memphis-dev");

(async function () {
    try {
        await memphis.connect({
            host: "localhost",
            username: "CLIENT_TYPE_USERNAME",
            password: "PASSWORD"
            accountId: ACCOUNT_ID //*optional* In case you are using Memphis.dev cloud
        });

        const consumer = await memphis.consumer({
            stationName: "marketing",
            consumerName: "cons1",
            consumerGroup: "cg_cons1",
            maxMsgDeliveries: 3,
            maxAckTimeMs: 2000,
            genUniqueSuffix: true
        });

        consumer.on("message", message => {
            console.log(message.getDataDeserialized());
            message.ack();
        });
        consumer.on("error", error => {
            console.log(error);
        });
    } catch (ex) {
        console.log(ex);
        memphis.close();
    }
})();

Last updated

Logo

All rights reserved to Memphis.dev 2023