Page cover image

Quick start

Create your first station, producer, and consumer in your preferred language.

To learn by practice, you can find a sample application here.

1. Which Memphis deployment are you using?

Step 1: Sign up for Memphis Cloud here.

Step 2: Hello world

2. Hello world

Step 1: Create an empty dir for the Node.js project

mkdir memphis-demo && \
cd memphis-demo

Step 2: Create a new Node project (If needed)

npm init -y

Step 3: Install memphis Node.js SDK

npm install memphis-dev

Step 4: Create a new .js file called producer.js

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

(async function () {
  let memphisConnection;

  try {
    memphisConnection = await memphis.connect({
      host: "MEMPHIS_BROKER_HOSTNAME",
      username: "APPLICATION_TYPE_USERNAME",
      password: "PASSWORD",
    });

    const producer = await memphisConnection.producer({
      stationName: "STATION_NAME",
      producerName: "PRODUCER_NAME",
    });

    const headers = memphis.headers();
    headers.add("KEY", "VALUE");
    await producer.produce({
      message: Buffer.from("Message: Hello world"), // you can also send JS object - {}
      headers: headers,
    });

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

Step 5: Run producer.js

node producer.js

Step 6: Create a new .js file called consumer.js

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

(async function () {
  let memphisConnection;

  try {
    memphisConnection = await memphis.connect({
      host: "MEMPHIS_BROKER_HOSTNAME",
      username: "APPLICATION_TYPE_USERNAME",
      password: "PASSWORD",
    });

    const consumer = await memphisConnection.consumer({
      stationName: "STATION_NAME",
      consumerName: "CONSUMER_NAME",
      consumerGroup: "CONSUMER_GROUP_NAME",
    });

    consumer.setContext({ key: "value" }); // Optional

    let messages = await consumer.fetch(); // Fetches 10 messages

    for (let message of messages){
        const messageObject = JSON.parse(message.getData().toString());
        // Do something with the message
        console.log(messageObject["Hello"]);
        message.ack();
    }

    memphisConnection.close();

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

Step 7: Run consumer.js

node consumer.js

Last updated

Logo

All rights reserved to Memphis.dev 2023