diff --git a/README.md b/README.md index abb4f81..5b690a6 100644 --- a/README.md +++ b/README.md @@ -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: "", + username: "", // (root/application type user) + 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: "" +}); +``` + +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:"", + producerName:"", + 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:"", + consumerName:"", +}); + +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`. diff --git a/examples/consumer.js b/examples/consumer.js index 1d0ae98..734940f 100644 --- a/examples/consumer.js +++ b/examples/consumer.js @@ -5,26 +5,32 @@ const { memphis } = require('memphis-dev'); try { memphisConnection = await memphis.connect({ - host: '', - username: '', - password: 'password', - accountId: '' // for cloud usage + host: "", + username: "memphis-username", // (root/application type user) + 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: "" }); - const consumer = await memphisConnection.consumer({ - stationName: '', - consumerName: '', - consumerGroup: '' - }); + let consumer = await memphis.consumer({ + stationName: "", + consumerName: "" + }) - 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(); diff --git a/examples/consumer.ts b/examples/consumer.ts index 103b389..351d831 100644 --- a/examples/consumer.ts +++ b/examples/consumer.ts @@ -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: '', - username: '', - password: 'password', - accountId: '' // for cloud usage + host: "", + username: "memphis-username", // (root/application type user) + 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: "" }); - const consumer = await memphisConnection.consumer({ - stationName: '', - consumerName: '', - consumerGroup: '' - }); + let consumer = await memphis.consumer({ + stationName: "", + consumerName: "" + }) - 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(); diff --git a/examples/producer.js b/examples/producer.js index a858921..795f519 100644 --- a/examples/producer.js +++ b/examples/producer.js @@ -1,29 +1,30 @@ const { memphis } = require("memphis-dev"); (async function () { - let memphisConnection + let memphisConnection; - try { + try { memphisConnection = await memphis.connect({ - host: '', - username: '', - password: 'password', - accountId: '' // for cloud usage + host: "", + username: "", // (root/application type user) + 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: "" }); - - const producer = await memphisConnection.producer({ - stationName: '', - producerName: '' - }); - - const headers = memphis.headers() - headers.add('', '') - await producer.produce({ - message: Buffer.from("Message: Hello world"), // you can also send JS object - {} - headers: headers + + let producer = await memphis.producer({ + stationName: "", + producerName: "" }); - - 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(); diff --git a/examples/producer.ts b/examples/producer.ts index ff32be6..7d5cc9f 100644 --- a/examples/producer.ts +++ b/examples/producer.ts @@ -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: '', - username: '', - password: 'password', - accountId: '' // for cloud usage + host: "", + username: "", // (root/application type user) + 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: "" }); - - const producer = await memphisConnection.producer({ - stationName: '', - producerName: '' + + let producer = await memphis.producer({ + stationName: "", + producerName: "" }); - - const headers = memphis.headers() - headers.add('', ''); + + 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();