Back to 2b site

8 Simple Steps to Create Pub/Sub on your Azure Environment

Pub/Sub is an asynchronous communication method where messages are exchanged between applications, and the most reliable way to communicate between microservices. There are three components of the communication.

  • Firstly, the end user or application, that sends Events or messages, and is also called the Publisher.
  • Next, the Topic, or multiple Topics. These are the intermediary, the centralized point where all the Events of a type are sent.
  • Lastly, the Subscriptions or multiple Subscriptions, (also called Subscriber) where a particular action is taken on the Event, such as alerting each Subscription as necessary.


What Are the Benefits of Using Pub/Sub?

 Pub/sub allows organizations to scale their messaging to a larger extent than they can achieve in a single traditional data center. This is mainly because different operations work simultaneously in parallel with one another, the messages use a caching system, and processes of intelligent routing are put in place.

Another benefit is the loose coupling of the different components. This means that the end-user or end-application is not directly connected to the Subscriptions, and can operate independently, sending messages or Events to the Topic, even if the Subscription is currently unavailable.

In this how-to, we will look at the smartest route to implementing pub/sub on Azure, in 8 steps, using Azure Function App and Service Bus services, and via creating an Azure API Management or local API (npm).


When you design a web app based on microservices architecture, you decide how to call those microservices from your app. For time-consuming services, you might want to use asynchronous service calls. pubsub is a way of reliable communication in microservice topology.


Getting Started

 There are a few pre-requisites that you will need for this approach.

  • Azure Function App (serverless) and Service Bus fundamentals.
  • First method – Azure API Management fundamentals.
  • Second method – make sure that npm is installed



pub sub Azure Architecture

 1. Create Function App:

First, head to the portal. We need to create a Function App service in Consumption plan with Monitoring (Application Insights) enabled:

create a Function App service in Consumption plan with Monitoring (Application Insights) enabled

2. Create Service Bus:

Next, create a Service Bus namespace from the portal:

Service Bus namespace

3. Inside the Service Bus, create a Topic and Subscription:

Azure Service Bus Topic creation

Inside the topic, go to Subscriptions and create one:

 Azure Service Bus Subscription creation with in Topic


4. Add a Function:

Inside Function App, go to functions, and create one. Next, choose “Azure Service Bus Topic Trigger” and your Service Bus, topic and subscription details.

Fanction creation in Inside Function App azure

5. Create an API:

This Microsoft documentation will help you to create an API from the portal. You can also create your own local API. In this demo, I am using a local API on Ubuntu Azure VM.

  • Install node.js (v14 LTS) & npm
  • Run “npm install -g json-server”
  • Create a file named ‘data.json’ and paste the following:

Create an API

Run “json-server –host <YOUR_IP> data.json”

Run json-server --host YOUR_IP data.json

Don’t forget to test your API:

GET requests: “curl http://<YOUR_IP>:3000/messagesTest Your API

POST requests: “curl -X POST http://<YOUR_IP>:3000/messages –data <JsonData>”Test Your API


6. Go into the function you created, Code+Test, and copy the following code:

code to sending a post request to our API (worker)

Using this code, we are actually sending a post request to our API (worker). In fact, we can use it for executing some operations and functions to the worker from this function for each trigger, which means for each event sent to the topic.

7. Go to Function App, in the left menu, go through Console.

Now, we want to prepare the environment and make sure our code will compile the way it should, and run as well. First of all, we will need to install the relevant modules we use in the code. In this case, we are using ‘request’ module. Inside the console, copy the following command: npm install request

8. It’s time to check our Pub/Sub!

Duplicate your browser tab. Open one tab on your Service Bus and open Function App on the other. Then go to your Topic and choose “Service Bus Explorer”, as you can see below:

pub sub Azure service bus

Here you are going to send the Event to the Topic which means the data will send to the API from the function (post request).

In the second tab, you can go to your function, then click on Monitor (Logs view):

Monitor (Logs view) pub sub Azure

On the first tab, click on ‘send’ to send the data to the Topic. Now, the data sent to our Topic and our function should listen to our Subscription. The function will handle the Event, and then execute the code which sends the data to your API.

You can check if it works using a GET request to the API like this:
“curl http://<YOUR_PUBLIC_IP>:3000/messages”


A Summary of How We Implemented the Pub/Sub Concept

 We used two Azure services to meet our goal, the Service Bus and the Function App. We set up an API that served as the worker for our application, and then we tested the entire flow. Along the way, we showed best practices for understanding and realizing the pub/sub concept as a whole, creating the most reliable way to communicate between microservices.

 Here at 2bcloud, we understand the minutia of handling a complex cloud environment, including setting up intelligent processes that will allow you to work flexibly, and with agility, speed and scale.

Want to discuss your unique cloud environment? Drop us a link below.