Service: SmoothMQ - EyevinnOSC/community GitHub Wiki

Getting started

The open source project SmoothMQ offers a drop-in-replacement for SQS. This tutorial walks you through using SmoothMQ in Open Source Cloud as a messaging queue in your solution, and using the Python boto3 client to push and read messages of the queue.

Prerequisites

  • If you have not already done so, sign up for an OSC account.
  • Python environment

Step 1: Install CLI

You will be using the CLI to create the service instances for this solution.

  1. If not already installed on your machine, download and install NodeJS
  2. Install OSC CLI using NodeJS package manager
% npm install -g @osaas/cli
  1. Verify that it is installed
% osc --version
0.11.2

Step 2: Setup secrets

Create two service secrets in the SmoothMQ service in the OSC web user interface.

  • accesskey=guide
  • secretkey=guide

The access key and secret to just serve as an example.

Step 3: Create message queue

Create a message queue service with this command in your terminal. We will refer to the service secrets created in step 2.

% osc create poundifdef-smoothmq guide -o AccessKey="{{secrets.accesskey}}" -o SecretKey="{{secrets.secretkey}}"

The message queue service is now created and available at the endpoint https://<tenant>-guide.poundifdef-smoothmq.auto.prod.osaas.io/ (where <tenant> is your tenant id).

Step 4: Create queue and send message with Python

To verify we will write a small Python application that creates a queue and place a message on the queue. Begin with setup a Python virtual environment.

% python -m venv .venv
% . .venv/bin/activate

Install the Boto3 client.

% python -m pip install boto3

Create a Python script we call send.py containing:

import boto3
sqs = boto3.client("sqs", endpoint_url="https://<tenant>-guide.poundifdef-smoothmq.auto.prod.osaas.io/")
sqs.create_queue(QueueName='guide')
response = sqs.list_queues()
url = response['QueueUrls'][0]
sqs.send_message(QueueUrl=url, MessageBody="Hello world")

Run the script with the access key and secret that we provided when creating the message queue service.

% export AWS_ACCESS_KEY_ID=guide
% export AWS_SECRET_ACCESS_KEY=guide
python send.py

Step 5: Read message from queue

Let us now develop a script we call recv.py containing:

import boto3
sqs = boto3.client("sqs", endpoint_url="https://<tenant>-guide.poundifdef-smoothmq.auto.prod.osaas.io/")
response = sqs.list_queues()
url = response['QueueUrls'][0]
msg = sqs.receive_message(QueueUrl=url)
print (msg['Messages'][0]['Body'])

And when running it we would get.

% python recv.py
Hello world
⚠️ **GitHub.com Fallback** ⚠️