Use queues and topics
Send and receive messages with SQS, SNS, Pub/Sub and Queue Storage, and find out what Service Bus and Event Hubs need.
You'll send a message into pogi and read it back, using a queue on each cloud, plus an SNS topic that delivers to a queue. Each section uses the command-line tool for that cloud.
Before you start
-
Run
pogi startin another terminal. -
Load the settings that point your tools at pogi:
eval "$(pogi env)" -
Install the tools for the sections you use: the AWS CLI,
gcloudand the Azure CLI (az). Azure Queue Storage listens on port 10001, the same as Azurite.
AWS: SQS and SNS
You'll send a message to an SQS queue and receive it. Then you'll subscribe the queue to an SNS topic, publish to the topic, and see the message arrive in the queue.
SQS
-
Make a queue:
QUEUE=$(aws sqs create-queue --queue-name orders --query QueueUrl --output text) echo $QUEUEOutput http://localhost:7644/000000000000/orders -
Send a message:
aws sqs send-message --queue-url $QUEUE --message-body "order 1" --query MessageId --output textOutput de4a5166-ead0-42bf-9b52-36208fd341c0 -
Receive it:
aws sqs receive-message --queue-url $QUEUE --query 'Messages[0].Body' --output textOutput order 1
SNS to SQS
-
Make a topic, and get the queue's ARN (the ID AWS gives a resource):
TOPIC=$(aws sns create-topic --name events --query TopicArn --output text) QARN=$(aws sqs get-queue-attributes --queue-url $QUEUE --attribute-names QueueArn --query Attributes.QueueArn --output text) echo $TOPIC echo $QARNOutput arn:aws:sns:us-east-1:000000000000:events arn:aws:sqs:us-east-1:000000000000:orders -
Subscribe the queue to the topic. Raw delivery puts your message in the queue as it is, without an SNS envelope around it:
aws sns subscribe --topic-arn $TOPIC --protocol sqs --notification-endpoint $QARN --attributes RawMessageDelivery=true --query SubscriptionArn --output textOutput arn:aws:sns:us-east-1:000000000000:events:52866301-3662-4ec2-9e77-3aeb62ae78d1 -
Publish to the topic:
aws sns publish --topic-arn $TOPIC --message "hello subscribers" --query MessageId --output textOutput 360fbb81-a89b-404d-88a1-0c939549bcf0 -
Receive the message from the queue:
aws sqs receive-message --queue-url $QUEUE --query 'Messages[].Body' --output textOutput hello subscribers
Google Cloud: Pub/Sub
You'll make a topic and a subscription (a named reader of the topic), publish a message and pull it.
-
Make a topic and a subscription:
gcloud pubsub topics create orders gcloud pubsub subscriptions create orders-sub --topic=ordersOutput Created topic [projects/pogi/topics/orders]. Created subscription [projects/pogi/subscriptions/orders-sub]. -
Publish a message:
gcloud pubsub topics publish orders --message="order 1"Output messageIds: - '1' -
Pull it.
--auto-ackacknowledges the message so it is not delivered again:gcloud pubsub subscriptions pull orders-sub --auto-ackOutput ┌─────────┬────────────┬──────────────┬────────────┬──────────────────┬────────────┐ │ DATA │ MESSAGE_ID │ ORDERING_KEY │ ATTRIBUTES │ DELIVERY_ATTEMPT │ ACK_STATUS │ ├─────────┼────────────┼──────────────┼────────────┼──────────────────┼────────────┤ │ order 1 │ 1 │ │ │ │ SUCCESS │ └─────────┴────────────┴──────────────┴────────────┴──────────────────┴────────────┘
Azure: Queue Storage
az storage works with the connection string that pogi env sets in AZURE_. It doesn't need az login. --only-show-errors hides the preview warning that az prints for these commands.
-
Make a queue:
az storage queue create --name orders --only-show-errorsOutput { "created": true } -
Put a message on it:
az storage message put --queue-name orders --content "order 1" --query content --output tsv --only-show-errorsOutput order 1 -
Get the message:
az storage message get --queue-name orders --query "[0].content" --output tsv --only-show-errorsOutput order 1
Azure: Service Bus and Event Hubs
Service Bus and Event Hubs send and receive over AMQP, which pogi serves on port 5672 when you start it with its default ports. az and curl can't send or receive on them, so these two need the official Azure SDK. There are no CLI steps for them here. pogi env sets no Service Bus or Event Hubs settings. The coverage page lists what each service supports.
Limits
- Pub/Sub does not support exactly-once delivery yet, and it has no BigQuery or Cloud Storage subscriptions.
- Queue Storage covers messages: put, get, peek, update, delete, visibility and time to live.
- Service Bus stores auto-delete-on-idle and maximum queue size but does not act on them. Event Hubs keeps AMQP and Kafka as separate logs, with a fixed partition count.
The coverage page lists every row: SQS and SNS, Pub/Sub and Azure.