Ask or search…
K
Links

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?

Cloud
Open-source
Step 1: Sign up for Memphis Cloud here.
Step 2: Hello world

Installation

For Kubernetes
Stable -
1
helm repo add memphis https://k8s.memphis.dev/charts/ --force-update &&
2
helm install memphis memphis/memphis --create-namespace --namespace memphis --wait --version=1.2.2
Latest -
1
helm repo add memphis https://k8s.memphis.dev/charts/ --force-update &&
2
helm install memphis memphis/memphis --create-namespace --namespace memphis --wait
More information can be found in the Memphis k8s deployment documentation.
Docker compose (Syntax for v2)
Stable -
curl -s https://memphisdev.github.io/memphis-docker/docker-compose.yml -o docker-compose.yml && docker compose -f docker-compose.yml -p memphis up
Latest -
curl -s https://memphisdev.github.io/memphis-docker/docker-compose-latest.yml -o docker-compose-latest.yml && docker compose -f docker-compose-latest.yml -p memphis up
More information can be found in the Memphis Docker deployment documentation.

2. Hello world

Node.js
TypeScript
Go
Python
C#
REST
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
1
const { memphis } = require("memphis-dev");
2
3
(async function () {
4
let memphisConnection;
5
6
try {
7
memphisConnection = await memphis.connect({
8
host: "MEMPHIS_BROKER_HOSTNAME",
9
username: "APPLICATION_TYPE_USERNAME",
10
password: "PASSWORD",
11
});
12
13
const producer = await memphisConnection.producer({
14
stationName: "STATION_NAME",
15
producerName: "PRODUCER_NAME",
16
});
17
18
const headers = memphis.headers();
19
headers.add("KEY", "VALUE");
20
await producer.produce({
21
message: Buffer.from("Message: Hello world"), // you can also send JS object - {}
22
headers: headers,
23
});
24
25
memphisConnection.close();
26
} catch (ex) {
27
console.log(ex);
28
if (memphisConnection) memphisConnection.close();
29
}
30
})();
Step 5: Run producer.js
node producer.js
Step 6: Create a new .js file called consumer.js
consumer.js
1
const { memphis } = require("memphis-dev");
2
3
(async function () {
4
let memphisConnection;
5
6
try {
7
memphisConnection = await memphis.connect({
8
host: "MEMPHIS_BROKER_HOSTNAME",
9
username: "APPLICATION_TYPE_USERNAME",
10
password: "PASSWORD",
11
});
12
13
const consumer = await memphisConnection.consumer({
14
stationName: "STATION_NAME",
15
consumerName: "CONSUMER_NAME",
16
consumerGroup: "CONSUMER_GROUP_NAME",
17
});
18
19
consumer.setContext({ key: "value" }); // Optional
20
21
let messages = await consumer.fetch(); // Fetches 10 messages
22
23
for (let message of messages){
24
const messageObject = JSON.parse(message.getData().toString());
25
// Do something with the message
26
console.log(messageObject["Hello"]);
27
message.ack();
28
}
29
30
memphisConnection.close();
31
32
} catch (ex) {
33
console.log(ex);
34
if (memphisConnection) memphisConnection.close();
35
}
36
})();
Step 7: Run consumer.js
node consumer.js
Step 1: Create an empty dir for the TypeScript 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 .ts file called producer.ts
producer.ts
1
import { memphis, Memphis } from "memphis-dev";
2
3
(async function () {
4
let memphisConnection: Memphis;
5
6
try {
7
memphisConnection = await memphis.connect({
8
host: "MEMPHIS_BROKER_HOSTNAME",
9
username: "APPLICATION_TYPE_USERNAME",
10
password: "PASSWORD",
11
});
12
13
const producer = await memphisConnection.producer({
14
stationName: "STATION_NAME",
15
producerName: "PRODUCER_NAME",
16
});
17
18
const headers = memphis.headers();
19
headers.add("key", "value");
20
await producer.produce({
21
message: Buffer.from("Message: Hello world"), // you can also send JS object - {}
22
headers: headers,
23
});
24
25
memphisConnection.close();
26
} catch (ex) {
27
console.log(ex);
28
if (memphisConnection) memphisConnection.close();
29
}
30
})();
Step 5: Run producer.ts
node producer.ts
Step 6: Create a new .ts file called consumer.ts
consumer.ts
1
import { memphis, Memphis, Message } from "memphis-dev";
2
3
(async function () {
4
let memphisConnection: Memphis;
5
6
try {
7
memphisConnection = await memphis.connect({
8
host: "MEMPHIS_BROKER_HOSTNAME",
9
username: "APPLICATION_TYPE_USERNAME",
10
password: "PASSWORD",
11
});
12
13
const consumer = await memphisConnection.consumer({
14
stationName: "STATION_NAME",
15
consumerName: "CONSUMER_NAME",
16
consumerGroup: "CONSUMER_GROUP_NAME",
17
});
18
19
consumer.setContext({ key: "value" });
20
21
let messages: Message[] = await consumer.fetch(); // Fetches 10 messages
22
23
for (let message of messages){
24
const messageObject: Record<string, any> = JSON.parse(message.getData().toString());
25
// Do something with the message
26
console.log(messageObject["Hello"]);
27
message.ack();
28
}
29
30
memphisConnection.close();
31
32
} catch (ex) {
33
console.log(ex);
34
if (memphisConnection) memphisConnection.close();
35
}
36
})();
Step 7: Run consumer.ts
node consumer.ts
Step 1: Create an empty dir for the Go project
mkdir memphis-demo && \
cd memphis-demo
Step 2: Init the newly created project
go mod init memphis-demo
Step 3: In your project's directory, install Memphis Go SDK
go get github.com/memphisdev/memphis.go
Step 4: Create a new Go file called producer.go
producer.go
1
package main
2
3
import (
4
"fmt"
5
"os"
6
7
"github.com/memphisdev/memphis.go"
8
)
9
10
func main() {
11
conn, err := memphis.Connect("MEMPHIS_HOSTNAME", "MEMPHIS_APPLICATION_USER", memphis.Password("PASSWORD"),)
12
if err != nil {
13
os.Exit(1)
14
}
15
defer conn.Close()
16
p, err := conn.CreateProducer("STATION_NAME", "PRODUCER_NAME")
17
18
hdrs := memphis.Headers{}
19
hdrs.New()
20
err = hdrs.Add("key", "value")
21
22
if err != nil {
23
fmt.Errorf("Header failed: %v", err)
24
os.Exit(1)
25
}
26
27
err = p.Produce([]byte("You have a message!"), memphis.MsgHeaders(hdrs))
28
29
if err != nil {
30
fmt.Errorf("Produce failed: %v", err)
31
os.Exit(1)
32
}
33
}
Step 4: Run producer.go
go run producer.go
Step 5: Create a new Go file called consumer.go
consumer.go
1
package main
2
3
import (
4
"fmt"
5
"context"
6
"os"
7
"time"
8
9
"github.com/memphisdev/memphis.go"
10
)
11
12
func main() {
13
conn, err := memphis.Connect("MEMPHIS_HOSTNAME", "MEMPHIS_APPLICATION_USER", memphis.Password("PASSWORD"),)
14
if err != nil {
15
os.Exit(1)
16
}
17
defer conn.Close()
18
19
consumer, err := conn.CreateConsumer("STATION_NAME", "CONSUMER_NAME", memphis.PullInterval(15*time.Second))
20
21
if err != nil {
22
fmt.Printf("Consumer creation failed: %v", err)
23
os.Exit(1)
24
}
25
26
consumer.SetContext(ctx)
27
28
messages, err := consumer.Fetch()
29
30
var msg_map map[string]any
31
for _, message := range messages{
32
err = json.Unmarshal(message.Data(), &msg_map)
33
if err != nil{
34
fmt.Print(err)
35
continue
36
}
37
38
// Do something with the message
39
40
err = message.Ack()
41
if err != nil{
42
fmt.Print(err)
43
continue
44
}
45
}
46
47
// The program will close the connection after 30 seconds,
48
// the message handler may be called after the connection closed
49
// so the handler may receive a timeout error
50
time.Sleep(30 * time.Second)
51
}
Step 6: Run consumer.go
go run consumer.go
Step 1: Create an empty dir for the Python project
mkdir memphis-demo && \
cd memphis-demo
Step 2: In your project's directory, install Memphis Python SDK
pip3 install --upgrade memphis-py
Step 3: Create a new Python file called producer.py
producer.py
1
from memphis import Memphis, Headers
2
from memphis.types import Retention, Storage
3
import asyncio
4
5
async def main():
6
try:
7
memphis = Memphis()
8
await memphis.connect(host="localhost", username="root", password="memphis")
9
producer = await memphis.producer(station_name="memphis-test", producer_name="producer-test")
10
await producer.produce(bytearray('Hello world', 'utf-8')) # you can send the message parameter as dict as well
11
12
except Exception as e:
13
print(e)
14
15
finally:
16
await memphis.close()
17
18
if __name__ == '__main__':
19
asyncio.run(main())
Step 4: Run producer.py
python3 producer.py
Step 5: Create a new Python file called consumer.py
consumer.py
1
from memphis import Memphis, Headers
2
from memphis.types import Retention, Storage
3
from memphis.message import Message
4
import asyncio
5
6
async def main():
7
try:
8
memphis = Memphis()
9
await memphis.connect(host="MEMPHIS_HOSTNAME", username="MEMPHIS_APPLICATION_USER", password="PASSWORD")
10
consumer = await memphis.consumer(station_name="STATION_NAME", consumer_name="CONSUMER_NAME", consumer_group="CONSUMER_GROUP_NAME")
11
12
messages: list[Message] = await consumer.fetch() # Type-hint the return here for LSP integration
13
14
for consumed_message in messages:
15
msg_data = json.loads(consumed_message.get_data())
16
17
# Do something with the message data
18
19
await consumed_message.ack()
20
21
except (MemphisError, MemphisConnectError) as e:
22
print(e)
23
24
finally:
25
await memphis.close()
26
27
if __name__ == '__main__':
28
asyncio.run(main())
Step 6: Run consumer.py
python3 consumer.py
Step 1: Create a new C# project through Visual Studio or using the dotnet CLI
dotnet new console -n MyC#Project
If you are using top level statements, you will have to create two of these projects.
Step 2: In your project's directory(s), install Memphis C# SDK
dotnet add package Memphis.Client -v ${MEMPHIS_CLIENT_VERSION}
Step 3: In your Producer file/project copy this quickstart inside of it
producer.cs
1
using Memphis.Client;
2
using System.Collections.Specialized;
3
using System.Text;
4
using System.Text.Json;
5
6
try
7
{
8
var options = MemphisClientFactory.GetDefaultOptions();
9
options.Host = "aws-us-east-1.cloud.memphis.dev";
10
options.AccountId = int.Parse(Environment.GetEnvironmentVariable("memphis_account_id"));
11
options.Username = "test_user";
12
options.Password = Environment.GetEnvironmentVariable("memphis_pass");
13
14
var memphisClient = await MemphisClientFactory.CreateClient(options);
15
16
var producer = await memphisClient.CreateProducer(
17
new Memphis.Client.Producer.MemphisProducerOptions
18
{
19
StationName = "test_station",
20
ProducerName = "producer"
21
});
22
23
Message message = new()
24
{
25
Hello = "World!"
26
};
27
28
var headers = new NameValueCollection();
29
30
for (int i = 0; i < 3; i++)
31
{
32
var msgBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message));
33
await producer.ProduceAsync(msgBytes, headers);
34
}
35
36
memphisClient.Dispose();
37
}
38
catch (Exception ex)
39
{
40
Console.Error.WriteLine(ex.Message);
41
}
42
43
public class Message
44
{
45
public string Hello { get; set; }
46
}
Step 4: Run producer.cs
dotnet run
Step 5: Create a new C# project/file called consumer.cs
consumer.cs
1
using Memphis.Client;
2
using Memphis.Client.Core;
3
using System.Text.Json;
4
5
try
6
{
7
var options = MemphisClientFactory.GetDefaultOptions();
8
options.Host = "aws-us-east-1.cloud.memphis.dev";
9
options.AccountId = int.Parse(Environment.GetEnvironmentVariable("memphis_account_id"));
10
options.Username = "test_user";
11
options.Password = Environment.GetEnvironmentVariable("memphis_pass");
12
13
var memphisClient = await MemphisClientFactory.CreateClient(options);
14
15
var consumer = await memphisClient.CreateConsumer(
16
new Memphis.Client.Consumer.MemphisConsumerOptions
17
{
18
StationName = "test_station",
19
ConsumerName = "consumer"
20
});
21
22
var messages = consumer.Fetch(3, false);
23
24
foreach (MemphisMessage message in messages)
25
{
26
var messageData = message.GetData();
27
var messageOBJ = JsonSerializer.Deserialize<Message>(messageData);
28
29
// Do something with the message here
30
Console.WriteLine(JsonSerializer.Serialize(messageOBJ));
31
32
message.Ack();
33
}
34
35
memphisClient.Dispose();
36
37
}
38
catch (Exception ex)
39
{
40
Console.Error.WriteLine(ex.Message);
41
}
42
43
public class Message
44
{
45
public string Hello { get; set; }
46
}
Step 6: Run consumer.cs
dotnet run
Producing messages to Memphis via REST API can be implemented using any REST-supported language like Go, Python, Java, Node.js, .NET, etc...
For the following tutorial, we will use Node.js .
Step 1: Create an empty dir for the REST API project
mkdir memphis-demo && \
cd memphis-demo
Step 2: Create a new Node project (If needed)
npm init -y
Step 3: Generate a new JWT token generate.js
generate.js
1
var axios = require("axios");
2
var data = JSON.stringify({
3
username: "APPLICATION_TYPE_USERNAME",
4
password: "PASSWORD",
5
token_expiry_in_minutes: 123,
6
refresh_token_expiry_in_minutes: 10000092,
7
});
8
9
var config = {
10
method: "post",
11
url: "localhost:4444",
12
headers: {
13
"Content-Type": "application/json",
14
},
15
data: data,
16
};
17
18
axios(config)
19
.then(function (response) {
20
console.log(JSON.stringify(response.data));
21
})
22
.catch(function (error) {
23
console.log(error);
24
});
Step 4: Run generate.js and copy the returned JWT
node generate.js
Step 5: Create a new file called producer.js
var axios = require("axios");
var data = JSON.stringify({
message: "New Message",
});
var config = {
method: "post",
url: "http://localhost:4444/stations/hps/produce/single",
headers: {
Authorization: "Bearer <jwt>",
"Content-Type": "application/json",
},
data: data,
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
More code examples here
Last modified 1mo ago