From d0a66b5f2438539eb804e2cf2cf5aff6bed0a29b Mon Sep 17 00:00:00 2001 From: John Peters Date: Tue, 26 Dec 2023 21:01:00 -0600 Subject: [PATCH 1/9] "Added quickstart section" --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/README.md b/README.md index c1c690e..f6fb428 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: "aws-us-east-1.cloud.memphis.dev", + username: "test_user", // (root/application type user) + accountId: process.env.memphis_account_id, //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: process.env.memphis_pass +}); +``` + +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:"test_station", + producerName:"producer", + 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:"test_station", + consumerName:"consumer", +}); + +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`. From 3ccc188598dbcf97c36a93d36d89d36808382f1c Mon Sep 17 00:00:00 2001 From: John Peters Date: Thu, 28 Dec 2023 20:33:40 -0600 Subject: [PATCH 2/9] "Updated exampels for javascript" --- examples/consumer.js | 37 ++++++++++++++++++++----------------- examples/producer.js | 42 ++++++++++++++++++++++-------------------- examples/producer.ts | 40 +++++++++++++++++++++------------------- 3 files changed, 63 insertions(+), 56 deletions(-) diff --git a/examples/consumer.js b/examples/consumer.js index 1d0ae98..5a26649 100644 --- a/examples/consumer.js +++ b/examples/consumer.js @@ -4,27 +4,30 @@ const { memphis } = require('memphis-dev'); let memphisConnection; try { - memphisConnection = await memphis.connect({ - host: '', - username: '', - password: 'password', - accountId: '' // for cloud usage + /* Javascript and typescript project */ + let memphisConnection = await memphis.connect({ + host: "aws-us-east-1.cloud.memphis.dev", + username: "test_user", // (root/application type user) + accountId: process.env.memphis_account_id, //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: process.env.memphis_pass }); - const consumer = await memphisConnection.consumer({ - stationName: '', - consumerName: '', - consumerGroup: '' - }); + let consumer = await memphis.consumer({ + stationName: "test_station", + consumerName: "consume" + }) - consumer.setContext({ key: "value" }); - consumer.on('message', (message, context) => { - console.log(message.getData().toString()); - message.ack(); - const headers = message.getHeaders() - }); + let messages = consumer.fetch() + + console.log(messages.length) + for (let message of messages){ + const messageObject = JSON.parse(message.getData().toString()); + // Do something with the message + console.log(messageObject["Hello"]); + message.ack() + } - consumer.on('error', (error) => { }); + memphisConnection.close() } catch (ex) { console.log(ex); if (memphisConnection) memphisConnection.close(); diff --git a/examples/producer.js b/examples/producer.js index a858921..dd2c225 100644 --- a/examples/producer.js +++ b/examples/producer.js @@ -1,29 +1,31 @@ const { memphis } = require("memphis-dev"); (async function () { - let memphisConnection + let memphisConnection; - try { + try { + /* Javascript and typescript project */ memphisConnection = await memphis.connect({ - host: '', - username: '', - password: 'password', - accountId: '' // for cloud usage + host: "aws-us-east-1.cloud.memphis.dev", + username: "test_user", // (root/application type user) + accountId: process.env.memphis_account_id, //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: process.env.memphis_pass }); - - 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 - }); - - memphisConnection.close(); + + let producer = await memphis.producer({ + stationName: "test_station", + producerName: "producer" + }) + + 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..dd2c225 100644 --- a/examples/producer.ts +++ b/examples/producer.ts @@ -1,29 +1,31 @@ -import { memphis, Memphis } from 'memphis-dev'; +const { memphis } = require("memphis-dev"); (async function () { - let memphisConnection: Memphis | null = null; + let memphisConnection; - try { + try { + /* Javascript and typescript project */ memphisConnection = await memphis.connect({ - host: '', - username: '', - password: 'password', - accountId: '' // for cloud usage + host: "aws-us-east-1.cloud.memphis.dev", + username: "test_user", // (root/application type user) + accountId: process.env.memphis_account_id, //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: process.env.memphis_pass }); - - const producer = await memphisConnection.producer({ - stationName: '', - producerName: '' - }); - - const headers = memphis.headers() - headers.add('', ''); + + let producer = await memphis.producer({ + stationName: "test_station", + producerName: "producer" + }) + + 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(); From dd200ad60144ed9140be16bb05c182d3eb35bf14 Mon Sep 17 00:00:00 2001 From: John Peters Date: Tue, 2 Jan 2024 09:43:19 -0600 Subject: [PATCH 3/9] "Changed params to generic params" --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f6fb428..6c710fc 100644 --- a/README.md +++ b/README.md @@ -69,10 +69,10 @@ const { memphis } = require('memphis-dev'); memphis = Memphis() let conn = await memphis.connect({ - host: "aws-us-east-1.cloud.memphis.dev", - username: "test_user", // (root/application type user) - accountId: process.env.memphis_account_id, //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: process.env.memphis_pass + 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: "" }); ``` @@ -80,8 +80,8 @@ Then, to produce a message, call the `memphis.produce` function or create a prod ```js await memphis.produce({ - stationName:"test_station", - producerName:"producer", + stationName:"", + producerName:"", message: { "Hello": "World" } @@ -94,8 +94,8 @@ Lastly, to consume this message, call the `memphis.fetch_messages` function or c ```js let messages = await memphis.fetchMessages({ - stationName:"test_station", - consumerName:"consumer", + stationName:"", + consumerName:"", }); for (let message of messages){ From 0e36cadf55edd3a8968f43ec7a579ba839cdfb24 Mon Sep 17 00:00:00 2001 From: John Peters Date: Thu, 4 Jan 2024 13:40:53 -0600 Subject: [PATCH 4/9] "Made examples use generic names" --- examples/consumer.js | 12 ++++++------ examples/producer.js | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/consumer.js b/examples/consumer.js index 5a26649..e8dc28a 100644 --- a/examples/consumer.js +++ b/examples/consumer.js @@ -6,15 +6,15 @@ const { memphis } = require('memphis-dev'); try { /* Javascript and typescript project */ let memphisConnection = await memphis.connect({ - host: "aws-us-east-1.cloud.memphis.dev", - username: "test_user", // (root/application type user) - accountId: process.env.memphis_account_id, //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: process.env.memphis_pass + 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: "" }); let consumer = await memphis.consumer({ - stationName: "test_station", - consumerName: "consume" + stationName: "", + consumerName: "" }) let messages = consumer.fetch() diff --git a/examples/producer.js b/examples/producer.js index dd2c225..ce787f4 100644 --- a/examples/producer.js +++ b/examples/producer.js @@ -6,15 +6,15 @@ const { memphis } = require("memphis-dev"); try { /* Javascript and typescript project */ memphisConnection = await memphis.connect({ - host: "aws-us-east-1.cloud.memphis.dev", - username: "test_user", // (root/application type user) - accountId: process.env.memphis_account_id, //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: process.env.memphis_pass + 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: "" }); let producer = await memphis.producer({ - stationName: "test_station", - producerName: "producer" + stationName: "", + producerName: "" }) for (let i = 0; i < 4; i++){ From 663c8c2ee4859a3aa1bfd510295a279166277b72 Mon Sep 17 00:00:00 2001 From: John Peters Date: Tue, 9 Jan 2024 14:41:34 -0600 Subject: [PATCH 5/9] "Consume now uses a for true in the example" --- examples/consumer.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/examples/consumer.js b/examples/consumer.js index e8dc28a..b96d58c 100644 --- a/examples/consumer.js +++ b/examples/consumer.js @@ -8,7 +8,7 @@ const { memphis } = require('memphis-dev'); let memphisConnection = await memphis.connect({ 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 + 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: "" }); @@ -17,17 +17,21 @@ const { memphis } = require('memphis-dev'); consumerName: "" }) - let messages = consumer.fetch() + while (true) { + let messages = consumer.fetch({}) - console.log(messages.length) - for (let message of messages){ - const messageObject = JSON.parse(message.getData().toString()); - // Do something with the message - console.log(messageObject["Hello"]); - message.ack() - } + 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(); + } - memphisConnection.close() + } } catch (ex) { console.log(ex); if (memphisConnection) memphisConnection.close(); From 8a8a2673158a3bdee35615e642ffc0df4435d203 Mon Sep 17 00:00:00 2001 From: John Peters Date: Wed, 10 Jan 2024 00:02:58 -0600 Subject: [PATCH 6/9] "AChanged the consumer.ts and producer.ts files" --- examples/consumer.ts | 47 ++++++++++++++++++++++++-------------------- examples/producer.ts | 2 +- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/examples/consumer.ts b/examples/consumer.ts index 103b389..a8a971c 100644 --- a/examples/consumer.ts +++ b/examples/consumer.ts @@ -1,32 +1,37 @@ -import { memphis, Memphis, Message } from 'memphis-dev'; +import { memphis, Memphis } from 'memphis-dev'; (async function () { - let memphisConnection: Memphis | null = null; + let memphisConnection; try { - memphisConnection = await memphis.connect({ - host: '', - username: '', - password: 'password', - accountId: '' // for cloud usage + /* Javascript and typescript project */ + let memphisConnection = await memphis.connect({ + 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.ts b/examples/producer.ts index dd2c225..4158b71 100644 --- a/examples/producer.ts +++ b/examples/producer.ts @@ -1,4 +1,4 @@ -const { memphis } = require("memphis-dev"); +import { memphis, Memphis } from 'memphis-dev'; (async function () { let memphisConnection; From e821bcf6a6ab933f83dd6b12ee7fbb316385f46e Mon Sep 17 00:00:00 2001 From: John Peters Date: Wed, 10 Jan 2024 00:03:53 -0600 Subject: [PATCH 7/9] "Generic variable names" --- examples/producer.js | 2 +- examples/producer.ts | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/producer.js b/examples/producer.js index ce787f4..ad425db 100644 --- a/examples/producer.js +++ b/examples/producer.js @@ -15,7 +15,7 @@ const { memphis } = require("memphis-dev"); let producer = await memphis.producer({ stationName: "", producerName: "" - }) + }); for (let i = 0; i < 4; i++){ await producer.produce({ diff --git a/examples/producer.ts b/examples/producer.ts index 4158b71..f9a1fd6 100644 --- a/examples/producer.ts +++ b/examples/producer.ts @@ -5,17 +5,17 @@ import { memphis, Memphis } from 'memphis-dev'; try { /* Javascript and typescript project */ - memphisConnection = await memphis.connect({ - host: "aws-us-east-1.cloud.memphis.dev", - username: "test_user", // (root/application type user) - accountId: process.env.memphis_account_id, //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: process.env.memphis_pass + let memphisConnection = await memphis.connect({ + 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: "" }); let producer = await memphis.producer({ - stationName: "test_station", - producerName: "producer" - }) + stationName: "", + producerName: "" + }); for (let i = 0; i < 4; i++){ await producer.produce({ From 63be4ddfeeb895069538958370eece370a5d9d16 Mon Sep 17 00:00:00 2001 From: John Peters Date: Wed, 10 Jan 2024 00:06:17 -0600 Subject: [PATCH 8/9] "Forgot a type annotation" --- examples/consumer.js | 2 +- examples/consumer.ts | 4 ++-- examples/producer.ts | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/consumer.js b/examples/consumer.js index b96d58c..7183f29 100644 --- a/examples/consumer.js +++ b/examples/consumer.js @@ -5,7 +5,7 @@ const { memphis } = require('memphis-dev'); try { /* Javascript and typescript project */ - let memphisConnection = await memphis.connect({ + memphisConnection = await memphis.connect({ 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 diff --git a/examples/consumer.ts b/examples/consumer.ts index a8a971c..2738e20 100644 --- a/examples/consumer.ts +++ b/examples/consumer.ts @@ -1,11 +1,11 @@ import { memphis, Memphis } from 'memphis-dev'; (async function () { - let memphisConnection; + let memphisConnection: Memphis; try { /* Javascript and typescript project */ - let memphisConnection = await memphis.connect({ + memphisConnection = await memphis.connect({ 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 diff --git a/examples/producer.ts b/examples/producer.ts index f9a1fd6..c8ac76a 100644 --- a/examples/producer.ts +++ b/examples/producer.ts @@ -1,14 +1,14 @@ import { memphis, Memphis } from 'memphis-dev'; (async function () { - let memphisConnection; + let memphisConnection: Memphis; try { /* Javascript and typescript project */ - let memphisConnection = await memphis.connect({ + memphisConnection = await memphis.connect({ 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 + 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: "" }); From 27b6e44597a419b25a4d2a0dd6eb5c8f1d046f3b Mon Sep 17 00:00:00 2001 From: John Peters Date: Mon, 15 Jan 2024 19:37:28 -0600 Subject: [PATCH 9/9] "Removed Javascript and typescript project" --- examples/consumer.js | 1 - examples/consumer.ts | 1 - examples/producer.js | 1 - examples/producer.ts | 1 - 4 files changed, 4 deletions(-) diff --git a/examples/consumer.js b/examples/consumer.js index 7183f29..734940f 100644 --- a/examples/consumer.js +++ b/examples/consumer.js @@ -4,7 +4,6 @@ const { memphis } = require('memphis-dev'); let memphisConnection; try { - /* Javascript and typescript project */ memphisConnection = await memphis.connect({ host: "", username: "memphis-username", // (root/application type user) diff --git a/examples/consumer.ts b/examples/consumer.ts index 2738e20..351d831 100644 --- a/examples/consumer.ts +++ b/examples/consumer.ts @@ -4,7 +4,6 @@ import { memphis, Memphis } from 'memphis-dev'; let memphisConnection: Memphis; try { - /* Javascript and typescript project */ memphisConnection = await memphis.connect({ host: "", username: "memphis-username", // (root/application type user) diff --git a/examples/producer.js b/examples/producer.js index ad425db..795f519 100644 --- a/examples/producer.js +++ b/examples/producer.js @@ -4,7 +4,6 @@ const { memphis } = require("memphis-dev"); let memphisConnection; try { - /* Javascript and typescript project */ memphisConnection = await memphis.connect({ host: "", username: "", // (root/application type user) diff --git a/examples/producer.ts b/examples/producer.ts index c8ac76a..7d5cc9f 100644 --- a/examples/producer.ts +++ b/examples/producer.ts @@ -4,7 +4,6 @@ import { memphis, Memphis } from 'memphis-dev'; let memphisConnection: Memphis; try { - /* Javascript and typescript project */ memphisConnection = await memphis.connect({ host: "", username: "", // (root/application type user)