Skip to content
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,60 @@ import { Module } from '@nestjs/common';
import { Memphis, MemphisModule, MemphisService } from 'memphis-dev';
```

### Quickstart - Producing and Consuming

The most basic functionaly of memphis is the ability to produce messages to a station and to consume those messages.

First, a connection to Memphis must be made:

```js
const { memphis } = require('memphis-dev');

// Connecting to the broker
memphis = Memphis()

let conn = await memphis.connect({
host: "<memphis-host>",
username: "<memphis-username>", // (root/application type user)
accountId: <memphis-accountid/>, // You can find it on the profile page in the Memphis UI. This field should be sent only on the cloud version of Memphis, otherwise it will be ignored
password: "<memphis-password>"
});
```

Then, to produce a message, call the `memphis.produce` function or create a producer and call its `producer.produce` function:

```js
await memphis.produce({
stationName:"<station-name>",
producerName:"<producer-name>",
message: {
"Hello": "World"
}
});

conn.close()
```

Lastly, to consume this message, call the `memphis.fetch_messages` function or create a consumer and call its `consumer.fetch` function:

```js
let messages = await memphis.fetchMessages({
stationName:"<station-name>",
consumerName:"<consumer-name>",
});

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

conn.close()
```

> Note: Remember to close the connection or the process won't exit!

### Connecting to Memphis

First, we need to connect with Memphis by using `memphis.connect`.
Expand Down
38 changes: 22 additions & 16 deletions examples/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,32 @@ const { memphis } = require('memphis-dev');

try {
memphisConnection = await memphis.connect({
host: '<memphis-host>',
username: '<application type username>',
password: 'password',
accountId: '<account-id>' // for cloud usage
host: "<memphis-host>",
username: "memphis-username", // (root/application type user)
accountId: <memphis-accountId />, //You can find it on the profile page in the Memphis UI. This field should be sent only on the cloud version of Memphis, otherwise it will be ignored
password: "<memphis-password>"
});

const consumer = await memphisConnection.consumer({
stationName: '<station-name>',
consumerName: '<consumer-name>',
consumerGroup: ''
});
let consumer = await memphis.consumer({
stationName: "<station-name>",
consumerName: "<consumer-name>"
})

consumer.setContext({ key: "value" });
consumer.on('message', (message, context) => {
console.log(message.getData().toString());
message.ack();
const headers = message.getHeaders()
});
while (true) {
let messages = consumer.fetch({})

if (messages.length == 0) {
continue;
}

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

consumer.on('error', (error) => { });
}
} catch (ex) {
console.log(ex);
if (memphisConnection) memphisConnection.close();
Expand Down
44 changes: 24 additions & 20 deletions examples/consumer.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import { memphis, Memphis, Message } from 'memphis-dev';
import { memphis, Memphis } from 'memphis-dev';

(async function () {
let memphisConnection: Memphis | null = null;
let memphisConnection: Memphis;

try {
memphisConnection = await memphis.connect({
host: '<memphis-host>',
username: '<application type username>',
password: 'password',
accountId: '<account-id>' // for cloud usage
host: "<memphis-host>",
username: "memphis-username", // (root/application type user)
accountId: <memphis-accountId />, //You can find it on the profile page in the Memphis UI. This field should be sent only on the cloud version of Memphis, otherwise it will be ignored
password: "<memphis-password>"
});

const consumer = await memphisConnection.consumer({
stationName: '<station-name>',
consumerName: '<consumer-name>',
consumerGroup: ''
});
let consumer = await memphis.consumer({
stationName: "<station-name>",
consumerName: "<consumer-name>"
})

consumer.setContext({ key: "value" });
consumer.on('message', (message: Message, context: object) => {
console.log(message.getData().toString());
message.ack();
const headers = message.getHeaders()
});
while (true) {
let messages = consumer.fetch({})

consumer.on('error', (error) => {
console.log(error);
});
if (messages.length == 0) {
continue;
}

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

}
} catch (ex) {
console.log(ex);
if (memphisConnection) memphisConnection.close();
Expand Down
39 changes: 20 additions & 19 deletions examples/producer.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
const { memphis } = require("memphis-dev");

(async function () {
let memphisConnection
let memphisConnection;

try {
try {
memphisConnection = await memphis.connect({
host: '<memphis-host>',
username: '<application type username>',
password: 'password',
accountId: '<account-id>' // for cloud usage
host: "<memphis-host>",
username: "<memphis-username>", // (root/application type user)
accountId: <memphis-accountId/>, //You can find it on the profile page in the Memphis UI. This field should be sent only on the cloud version of Memphis, otherwise it will be ignored
password: "<memphis-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

let producer = await memphis.producer({
stationName: "<station-name>",
producerName: "<producer-name>"
});

memphisConnection.close();

for (let i = 0; i < 4; i++){
await producer.produce({
message: {
"Hello": "World"
}
});
}

memphisConnection.close()
} catch (ex) {
console.log(ex);
if (memphisConnection) memphisConnection.close();
Expand Down
35 changes: 18 additions & 17 deletions examples/producer.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { memphis, Memphis } from 'memphis-dev';

(async function () {
let memphisConnection: Memphis | null = null;
let memphisConnection: Memphis;

try {
try {
memphisConnection = await memphis.connect({
host: '<memphis-host>',
username: '<application type username>',
password: 'password',
accountId: '<account-id>' // for cloud usage
host: "<memphis-host>",
username: "<memphis-username>", // (root/application type user)
accountId: <memphis-accountId/>, //You can find it on the profile page in the Memphis UI. This field should be sent only on the cloud version of Memphis, otherwise it will be ignored
password: "<memphis-password>"
});

const producer = await memphisConnection.producer({
stationName: '<station-name>',
producerName: '<producer-name>'
let producer = await memphis.producer({
stationName: "<station-name>",
producerName: "<producer-name>"
});

const headers = memphis.headers()
headers.add('<key>', '<value>');

for (let i = 0; i < 4; i++){
await producer.produce({
message: Buffer.from("Message: Hello world"), // you can also send JS object - {}
headers: headers
message: {
"Hello": "World"
}
});

memphisConnection.close();
}

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