VOOZH about

URL: https://thenewstack.io/golang-pub-sub-why-its-better-when-combined-with-gofr/

⇱ Golang Pub/Sub: Why It’s Better When Combined With GoFr - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-08-28 06:00:53
Golang Pub/Sub: Why It’s Better When Combined With GoFr
tutorial,
Data Streaming / Edge Computing / Go

Golang Pub/Sub: Why It’s Better When Combined With GoFr

To take full advantage of Golang’s capabilities in a pub/sub setup, the framework GoFr can help simplify the process and introduce powerful features.
Aug 28th, 2024 6:00am by Robert Kimani
👁 Featued image for: Golang Pub/Sub: Why It’s Better When Combined With GoFr
As modern systems evolve, the need for reliable, scalable and real-time communication has never been greater. Pub/sub (publish-subscribe) is a messaging pattern that allows different components of a system to communicate asynchronously. This decoupled architecture is the backbone of the Internet of Things (IoT), distributed systems and real-time applications where responsiveness and flexibility are key. When building these systems, Golang is an obvious choice due to its simplicity, efficiency and built-in concurrency. But to take full advantage of Golang’s capabilities in a pub/sub setup, the framework GoFr offers optimized solutions that simplify the process and introduce powerful features. Leading companies have successfully implemented pub/sub systems using frameworks like GoFr to solve complex challenges. For instance, LinkedIn, Pinterest and Walmart have all leveraged event-driven architectures and pub/sub to manage massive amounts of data and ensure system reliability. GoFr offers a powerful set of tools and features that elevate Golang’s pub/sub capabilities, making it an ideal choice for building scalable, real-time systems, especially in the IoT domain. By choosing GoFr, developers can benefit from a proven framework that simplifies the development process and ensures that their pub/sub systems are reliable and easy to manage. Event-driven architecture (EDA) lies at the core of modern, scalable and resilient real-time systems. Unlike the traditional request-response model, where services communicate synchronously, EDA allows for asynchronous communication. This decouples services, enabling them to operate independently and respond to events in real time. For IoT applications, where devices continuously generate and exchange data, pub/sub becomes a critical communication mechanism. By implementing pub/sub, IoT systems can handle a large number of devices, ensuring scalability, reliability and real-time responsiveness. Pub/sub’s ability to handle high-throughput, low-latency communication is particularly valuable in these environments. In this article, I’ll show you why GoFr, combined with Golang, is the perfect match for building high-performance pub/sub systems and how you can get started quickly, with an IoT example using the communication protocol MQTT.

Why Choose GoFr for Pub/Sub in Golang?

Golang has risen to prominence in building distributed systems because of its impressive performance and concurrency model. Pub/sub architectures benefit significantly from Go’s goroutines, which allow lightweight, asynchronous communication between different services without introducing significant overhead. This is crucial in systems where multiple events must be processed concurrently. Here’s why GoFr is an ideal choice for developers building IoT systems and other real-time apps in Go:
  • Simplicity and efficiency. GoFr abstracts much of the boilerplate code associated with setting up pub/sub, allowing developers to focus on business logic rather than infrastructure management.
  • Support for multiple message brokers. GoFr natively supports a variety of message brokers, including Apache Kafka, Google pub/sub, and MQTT. This flexibility ensures that developers can choose the best broker for their specific use case.
  • Comprehensive monitoring and security. With built-in monitoring and security features, GoFr ensures that your pub/sub system is not only efficient but also secure and easy to manage.
  • Optimized for IoT with MQTT. MQTT is a lightweight messaging protocol designed for IoT, and GoFr’s support for MQTT makes it an excellent choice for IoT backends. GoFr simplifies the setup and management of MQTT brokers, allowing for seamless integration into your IoT systems.
  • Routing and middleware. Simplifies setting up REST APIs with built-in route handling and middleware.
  • Database support. Easily connects to SQL, NoSQL, and time series databases for data storage and processing.

Optimizing Pub/Sub With GoFr

GoFr is built with scalability and ease of use in mind. It provides native support for MQTT, one of the most popular protocols for real-time communication in IoT systems. By leveraging GoFr’s built-in pub/sub capabilities, you can set up a robust system with minimal effort. Here’s how you can set up a simple MQTT-based pub/sub system using GoFr.

Setting Up the Development Environment

First, initialize your Go module and add the GoFr package to your project:
go mod init github.com/gofr_iot_project
go get gofr.dev

Example Code for Publishing Messages

This simple example sets up a REST API endpoint `/light` that receives data about a smart light’s mode and publishes it to the MQTT topic `room-smart-light`. Any IoT devices subscribed to this topic will receive the message. You can connect to an MQTT broker by adding the following configuration in your `.env` file:
PUBSUB_BACKEND=MQTT
In the configuration line PUBSUB_BACKEND=MQTT, there’s no need to specify additional credentials such as an ID or password because we are connecting to a public MQTT broker. Public brokers, unlike private or secured brokers, don’t require authentication details such as usernames or passwords. This makes it easier to get started and test out your system without worrying about setting up complex security configurations. However, for production environments or more sensitive applications, connecting to a private broker would typically require credentials to ensure secure communication. Additionally, GoFr simplifies tracing and monitoring by providing a built-in tracer endpoint. This tracer allows you to monitor the flow of data in real time, track event life cycles and identify performance bottlenecks or errors as they occur. This level of visibility is crucial when scaling systems or troubleshooting issues, as it helps you maintain system health and ensure events are processed as expected.

Example Code for Subscribing to Topics

Similarly, you can create a subscriber that listens to topics and processes incoming messages:

High-Level Architecture for IoT Backends with GoFr

When designing an IoT backend with GoFr, the architecture typically involves several key components:
  • API Gateway. The API Gateway acts as the central point for managing and routing API requests. With GoFr, setting up a CRUD API is simplified, thanks to features like `AddRESTHandlers`, which automates route handling and database integration.
  • Device management. Secure and scalable device management is crucial for IoT systems. GoFr includes built-in authentication middleware that supports multiple mechanisms like OAuth and Basic-Auth, ensuring secure communication between services.
  • Data ingestion and processing. GoFr supports real-time data handling through MQTT and HTTP. It also integrates seamlessly with various databases, making it easy to store and process large volumes of data.
  • Monitoring and security. Robust monitoring and security are critical for any IoT backend. GoFr offers comprehensive monitoring capabilities, providing insights into system performance and security.
GoFr is built with scalability and fault tolerance at its core, making it highly suitable for handling high-throughput systems like IoT. It includes essential features such as retry mechanisms, dead-letter queues and circuit breakers, which ensure that even under heavy load or in the event of component failures, the system remains resilient. Dead-letter queues catch unprocessable messages and move them to a separate queue for further inspection, allowing operators to handle exceptions in a controlled manner. Circuit breakers prevent cascading failures by halting communication with malfunctioning services until they recover, thus minimizing the impact of individual service failures on the overall system. Security is another key consideration, and GoFr supports various authentication mechanisms, including OAuth, Basic Auth, and other secure communication methods, ensuring that data is transmitted securely between services. Try using GoFr for your pub/sub systems built with Go, and see if you don’t reap the benefits I’ve described.
TRENDING STORIES
Robert is a systems engineer and open source advocate who loves sharing knowledge. He believes in helping others and is compassionate about giving back to the community. When he’s not geeking with Linux he likes hiking, mountain biking, and exploring...
Read more from Robert Kimani
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.