Connecting Kafka and PubNub
Published: 2024-03-05 16:20:00

Kafka is fast becoming the defacto Event Broker for Event-driven architecture (EDA) development. EDA is helping accelerate software development, generate real-time functionality and insights, and decouple monolithic systems.

But Kakfa is not designed to directly connect with [edge devices](https://www.pubnub.com/solutions/edge-messaging/), like smartphones, browsers, IoT devices, and laptops. That’s where PubNub comes in.  PubNub is a complete platform for building & managing real-time apps that run on edge devices that Kafka can’t reach. Luckily, our customers can have the best of both worlds, powering their real-time apps with data that comes directly from their systems (where events are already streaming through Kafka) and connecting to their edge devices using PubNub.

Also, many of our customers will stream events from edge devices back into Kafka, with optional filtering, aggregation, and other pre-processing needed “at the edge” (i.e., within PubNub’s network) before those events are sent to Kafka.

What is the PubNub Kafka Bridge?
--------------------------------

The [PubNub Kafka Bridge](https://www.pubnub.com/developers/kafka/) enables you to seamlessly bridge events with your Kafka deployment into and out of PubNub using our new [Sink Connector](https://www.pubnub.com/docs/general/integrations/kafka/pubnub-kafka-sink-connector) and [Kafka Action](https://www.pubnub.com/docs/serverless/events-and-actions/actions/create-kafka-action), respectively. The Kafka bridge supports deployment on-premises, on Amazon through their Managed Kafka service (MSK), or on Confluent cloud.

The ready-made Kafka Sink Connector integrates directly with your Kafka deployment, enabling real-time event distribution **from** Kafka **to** edge devices, including web browsers, mobile apps, and IoT sensors. 

The fully managed Kafka Action, accessible within our Events & Actions service, facilitates streaming events **from** edge devices back **into** Kafka, offering optional filtering, aggregation, and other pre-processing capabilities needed “at the edge.”

![](https://www.pubnub.com/cdn/3prze68gbwl1/2NylVGyDwejUVytkpbDjve/5b4e989f27a501a2c2063c54850d73eb/Kafka_Bridge.svg?w=700&h=550)

If you just want to know how to configure the PubNub Kafka Bridge, feel free to skip straight to the end of this article or check out our dedicated [Kafka documentation](https://www.pubnub.com/docs/general/integrations/kafka/basics).

Why use PubNub + Kafka for your Event Driven Architecture (EDA)?
----------------------------------------------------------------

Long story short: Kafka is the most popular solution to power event-driven architecture inside the data center. PubNub does not replace Kafka but extends it outside of the data center to end-user devices and other data centers. 

**But what does that mean in practice?**

All successful large companies have made the transition to an event-driven architecture to improve the management and scalability of their systems, delivering a more responsive experience to customers.  EDA uses events to communicate between back-end services and/or end-user devices.  Think of an event as a change in system state, like a new order being placed through an e-commerce website or a status update received from a field worker.  These events are published onto an event bus, and then routed through to anyone who has subscribed to receive those events.  

Events, Event Bus, and Subscribers are generic terms, and you might see different terms used to describe functionally similar concepts, for example, the “Event Bus” is variously known as the “Message Bus,” ‘Event Broker,” or “Pub/Sub System,” depending on your source or industry.

**Why Kafka?**

[Kafka](https://kafka.apache.org) is a reliable, open-source EDA solution that 80% of Fortune 100 companies use. 

**So, why use PubNub with Kafka?**

While Kafka is used and deployed within the customer’s data center, PubNub extends the event-driven concepts of EDA outside of the data center to the [network edge](https://www.pubnub.com/solutions/edge-messaging/), including to web browsers, mobile apps, and IoT devices.  Both Kafka and PubNub are designed with scale in mind, so when used in conjunction, can connect millions of end-user devices outside the firewall with hundreds of backend servers within a secure network.

Additionally, using a single service such as PubNub to communicate with your edge devices can simplify your certification process, since you no longer have to perform a vendor check for every service/device you need to send events to/from - you only need to do a single vendor check, with PubNub.

Think of PubNub as your “global event bus” or message bus, enabling you to expand your Kafka EDA deployment from centralized data centers to a broad range of edge devices.  PubNub already supports all event bus features, such as [Pub/Sub messaging](https://www.pubnub.com/docs/general/messages/publish) for real-time event streaming, [enhanced security](https://www.pubnub.com/docs/general/security/access-control), [persistence](https://www.pubnub.com/docs/general/storage) to retrieve past messages, and [on-the-fly event processing](https://www.pubnub.com/docs/serverless/functions/overview).  

-   **Are you generating Kafka events that you would like to see at your network edge?**  You can achieve that with PubNub.
    
-   **Are you gathering data at the network edge?**  For example, users interacting with your mobile or web application?  You can send these events to your Kafka instance using PubNub
    

PubNub + Kafka: EDA use cases:
------------------------------

Let’s look at some use cases that can be achieved using a combination of PubNub and Kafka:

-   Any Kafka event you are already generating within your system can be **streamed in real-time** directly to applications, including JavaScript apps running in the browser.  You can filter and process the Kafka events before they are published, ensuring that only important and relevant events are sent.
    
-   **Extend your Kafka events to the network edge** in a secure way that doesn’t require specialized custom gateways to be running within your environment.
    
-   **Connect heterogeneous systems with ease:** PubNub has over 50+ SDKs and is built from the ground up with [security](https://www.pubnub.com/docs/general/setup/data-security) in mind, so interoperability will not be a concern.
    
-   **Build apps that stream real-time events** directly to each other (like large chat rooms, device locations, sensor readings, etc.) while still ensuring all events stream simultaneously into your Kafka broker for analysis, archiving, and more.
    
-   **Bridge multiple data centers** or hybrid data center deployments without setting up complex VPC environments.
    
-   **Syndicate data streams to business partners**, exposing a secure streaming API based on PubNub, with a zero-trust security model that allows you to filter, configure, and disable event streams with ease.
    

Configuring the PubNub Kafka Bridge
-----------------------------------

The [Kafka Bridge](https://www.pubnub.com/docs/general/integrations/kafka/basics) is PubNub’s umbrella term for all Kafka/PubNub integration and comprises two separate solutions.

1.  The “PubNub Kafka Sink Connector” is used to **send Kafka events** **_into_** **PubNub**.  We provide a custom [Kafka Sink Connector](https://kafka.apache.org/documentation/#sinkconnectconfigs) that allows PubNub to receive Kafka events. 
    
2.  The “PubNub Kafka Action,” which is part of our [Events & Actions](https://www.pubnub.com/docs/serverless/events-and-actions/overview) product, enables you to **send PubNub events** **_into_** **Kafka**.
    

As a prerequisite to any configuration steps detailed here, you will need to [sign up to PubNub](http://admin.pubnub.com) and obtain your API keys (publish\_key and subscribe\_key).

### Configuring the PubNub Kafka Sink Connector

To better understand how to install, configure, and test the PubNub Kafka Sink connector, **I would strongly recommend this earlier blog post, ‘**[**Installing the PubNub Kafka Sink Connector**](https://www.pubnub.com/blog/installing-pubnub-kafka-sink-connector/)**,’** written by the developer of the connector.

You can also find additional documentation on our [PubNub Kafka Sink Connector docs page](https://www.pubnub.com/docs/general/integrations/kafka/pubnub-kafka-sink-connector).

In summary: the PubNub Kafka sink connector is available on Github at [https://github.com/pubnub/pubnub-kafka-sink-connector](https://github.com/pubnub/pubnub-kafka-sink-connector).  

If you just want to test the connector to see how it works, then a Docker image is provided within the repository, which will spin up a Kafka instance, install dependencies, and emit test events consisting of a timestamp parameter.  After enabling the connector using a POST call, you will see PubNub events being received with low latency that match Kafka test events. 

To use the sink connector in production, a Maven command can be used to build the .jar file from the source, which can then be copied into the appropriate directory (depending on your [Kafka Connect setup](https://docs.confluent.io/platform/current/connect/userguide.html#installing-kconnect-plugins)) and the connector configured accordingly.

Please see the documentation page for the [complete connector configuration](https://www.pubnub.com/docs/general/integrations/kafka/pubnub-kafka-sink-connector#configuration-options), but all deployments will need, at minimum:

-   The Kafka topic or topics that you want to send to PubNub
    
-   The PubNub keys and ID that Kafka should use when publishing to PubNub 
    
-   Additional options for serialization and error handling.
    

### Configuring the PubNub Kafka Action

[Events & Actions](https://www.pubnub.com/docs/serverless/events-and-actions/overview) is a PubNub service that allows you to send real-time data to third-party systems for storage, processing, or analytics.  Events & Actions works by allowing you to specify PubNub [events](https://www.pubnub.com/docs/serverless/events-and-actions/events), for example, a message was published to a specific PubNub channel, or a user with a particular ID publishes a message.  For each event, you can specify which [Actions](https://www.pubnub.com/docs/serverless/events-and-actions/overview#actions) should take place whenever the event is triggered, for example, you might want to trigger a Webhook, send your data to Amazon Kinesis, or (as you might have guessed from the topic of this article) send your data to Apache Kafka. 

As a quick reference to the documentation, the steps are:

1.  [Define the PubNub event or events](https://www.pubnub.com/docs/serverless/events-and-actions/events) that represent the data you want to send to Kafka.  This is also the step that allows you to filter for events that match specific patterns
    
2.  [Configure the Kafka action](https://www.pubnub.com/docs/serverless/events-and-actions/actions/create-kafka-action) with details of your Kafka endpoint.  This could be self-hosted, an Amazon MSK cluster, or Confluent Cloud.
    

### Example configuring the PubNub Kafka Action

By selecting ‘Events & Actions’ from the [PubNub admin portal](https://admin.pubnub.com), you have the ability to specify both an event listener and an action. 

![Creating a Kafka Action ](https://www.pubnub.com/cdn/3prze68gbwl1/6OVcygGditdFc2EGSVIy1j/9f1f2cd9030f27e9efba8cc5a39705c5/1.png)

Select ‘+ Add Event Listener’ and choose an Event source.

![Creating a Kafka Action ](https://www.pubnub.com/cdn/3prze68gbwl1/52VGEHf6TQFvPE3I41pb6g/b533f7787d2688c741cd694cece10cbe/2.png)

Let’s assume we want to export all messages received on the channel whose name is "send-to-kafka," the event configuration will look something like this:

![Creating a Kafka Action ](https://www.pubnub.com/cdn/3prze68gbwl1/5Oh144gN0jimJfKaYxLgbB/39e902cc2bfe0e9db80f2d1fa7960bd5/3.png)

Next, Create an Apache Kafka action

![Creating a Kafka Action ](https://www.pubnub.com/cdn/3prze68gbwl1/1IDJ8MfST0SAAcRtUpxXRC/ee64a3d56d4e4b6ff47711f8dde3ec13/4.png)

What each of the fields in the action configuration means is detailed in the [Kafka action documentation](https://www.pubnub.com/docs/serverless/events-and-actions/actions/create-kafka-action), but the following screenshot matches the configuration for:

-   Kafka topic: topic\_0
    
-   Kafka topic key: stamford
    
-   Authentication: SCRAM-SHA-256
    
-   URL of the Kafka cluster to receive events: 574mf0rd-bridge.ldn-west:1905
    
-   Failed attempts will retry twice, with an interval calculated using a [formula](https://www.pubnub.com/docs/serverless/events-and-actions/events#retries) based around 450 seconds 
    

![Creating a Kafka Action ](https://www.pubnub.com/cdn/3prze68gbwl1/5htBDRFrFIPAIrJmADnK73/fad12a93dca4f892d4dd166a656f9722/5.png)

In Summary
----------

If you are already using Kafka in your organization, you can enhance the real-time capabilities of devices at your [network edge using PubNub](https://www.pubnub.com/solutions/edge-messaging/).  This means that events happening on your Kafka event bus can be distributed to your clients regardless of the platform, for example, a web browser using JavaScript or a mobile device running iOS or Android.  You can also ingest data from your client devices into Kafka in real-time over the PubNub network without any complex setup, scalability, or data security concerns.

To learn more, check out our dedicated [Kafka Bridge developers page](https://www.pubnub.com/developers/kafka/) or our [Kafka integration documentation](https://www.pubnub.com/docs/general/integrations/kafka/basics).  If you have any questions about setup, then please contact our [support team](https://support.pubnub.com/hc/en-us) or our developer relations team by emailing [devrel@pubnub.com](mailto:devrel@pubnub.com).

#### Contents

[What is the PubNub Kafka Bridge?](#h-0)[Why use PubNub + Kafka for your Event Driven Architecture (EDA)?](#h-1)[PubNub + Kafka: EDA use cases:](#h-2)[Configuring the PubNub Kafka Bridge](#h-3)[Configuring the PubNub Kafka Sink Connector](#h-4)[Configuring the PubNub Kafka Action](#h-5)[Example configuring the PubNub Kafka Action](#h-6)[In Summary](#h-7)

How can PubNub help you?
========================

This article was originally published on [PubNub.com](https://www.pubnub.com/blog/connecting-kafka-and-pubnub/)

Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.

The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.

Experience PubNub
-----------------

Check out [Live Tour](https://www.pubnub.com/tour/introduction/) to understand the essential concepts behind every PubNub-powered app in less than 5 minutes

Get Setup
---------

Sign up for a [PubNub account](https://admin.pubnub.com/signup/) for immediate access to PubNub keys for free

Get Started
-----------

The [PubNub docs](https://www.pubnub.com/docs) will get you up and running, regardless of your use case or [SDK](https://www.pubnub.com/docs)