DEV Community


Introduction to Dapr Source Code With a Pub-Sub Sample

abserari profile image AbserAri Updated on ・4 min read

Introduction to Dapr Source Code With a Pub-Sub Sample


Hello guys, I'll tell you about the Dapr's source code. Dapr is a distributed application runtime. On its official website. It's introduced as: An event-driven, portable runtime for building microservices on cloud and edge. I have followed Dapr for almost one year. When it was published to the public, I wrote two blogs to introduce how I got familiar with Dapr and recorded my steps.

Today, I'm curious about how Dapr have these features: portable, event-driven, etc. so I dig into the source code and wonder if I could learn something from its architecture.

I drew some pictures and stored them here. Hope they are helpful.Alt Text

Below we would run a sample called pub-sub sample and explain how the components like pub-sub could be portable to Dapr.


We need to get Dapr installed. If not, follow or my blog. If you have Mac

$ curl -fsSL | /bin/bash
 $  dapr init
⌛  Making the jump to hyperspace...
    Downloading binaries and setting up
✅  Success! Dapr is up and running

hint: you should have docker installed, too.

Get the code

$ git clone

I wrote a simpler example in

SDK-Source Code


In the configuration, we specify the metadata of the component, like our pub-sub component below:

kind: Component
  name: messagebus
  type: pubsub.redis
  - name: redisHost
  - name: redisPassword
    value: "123456"

name: messagebus

Dapr needs the name to configure the API
Alt Text


This config is to init the components so Dapr could connect.


After we init the Dapr runtime. we could use pub-sub with go-sdk quickly.
We use this function to publish data to target pub-sub components with pubsubName and specific topicName.

client.PublishEvent(ctx, pubsubName, topicName, data)


It's a little unfamiliar with the way of subscribing to Dapr. you should have a server with some handler to tell Dapr runtime what topic you care about and how to notify you with the specific route when Dapr has a message to publish to the topic your concern.

config subscription

var sub := &common.Subscription{
    PubsubName: "messagebus",
    Topic:      "neworder",
    Route:      "/orders",

add to server

if err := s.AddTopicEventHandler(sub, eventHandler); err != nil {
    log.Fatalf("error adding topic subscription: %v", err)


This folder contains two go files that use this go-SDK to invoke the Dapr PubSub API.


Alt Text



  • Get dapr installed

Run Subscriber Server

When we use Dapr PubSub to subscribe, we should have an http or gRPC server to receive the requests from Dapr.
Please change directory to pubsub/ and run the following command:

dapr run --app-id sub \ 
         --app-protocol http \ 
         --app-port 8080 \ 
         --port 3500 \ 
         --log-level debug \ 
         --components-path ./config \ 
         go run sub.go

Run Publisher

Publish is more simple than subscribe. Just Publish the data to target pubsub component with its' name.
After you start a server by above guide. Please change directory to pubsub/ and run the following command:

dapr run --app-id pub \ 
         --log-level debug \ 
         --components-path ./config \ 
         go run pub.go


You would see the log that in the terminal which runs the server(subscriber) code.

== APP == 2020/08/23 13:21:58 event - PubsubName: messagebus, Topic: demo, ID: 11acaa82-23c4-4244-8969-7360dae52e5d, Data: ping

Dapr Source Code


We have to know about the Dapr runtime in the real world. then we tell about how Dapr runtime registers the components.Alt Text

It's an interface in Dapr source code and the different realize of components is in

Then Dapr has a registry struct to store the method that use to new a component instance.


When we run the dapr. runtime init all the components with YAML files in your path. if in Kubernetes mode, it would read the yaml from Kubernetes.
Alt Text

I'm happy to chat with your question on dapr or this article. Please comment or contact me.

Discussion (1)

Editor guide