---
title: Ingest from Google Pub/Sub
meta:
  description: In this guide you'll learn how to send data from Google Pub/Sub to Tinybird.
---

# Stream from Google Pub/Sub

Learn how to send data from Google Pub/Sub to Tinybird.

## Overview

[Google Pub/Sub](https://cloud.google.com/pubsub) is often used as a messaging middleware that decouples event stream sources from the end destination. Pub/Sub streams are usually consumed by Google's DataFlow which can send events on to destinations such as BigQuery, BigTable, or Google Cloud Storage.

This DataFlow pattern works with Tinybird too, however, Pub/Sub also has a feature called [Push subscriptions](https://cloud.google.com/pubsub/docs/push) which can forward messages directly from Pub/Sub to Tinybird. The following guide steps use the subscription approach.

## Push messages from Pub/Sub to Tinybird

### 1. Create a Pub/Sub topic

Start by creating a topic in Google Pub/Sub following the [Google Pub/Sub documentation](https://cloud.google.com/pubsub/docs/admin#create_a_topic).

### 2. Create a push subscription

Next, [create a Push subscription in Pub/Sub](https://cloud.google.com/pubsub/docs/create-push-subscription).

Set the **Delivery Type** to **Push**.

In the **Endpoint URL** field, ue the following snippet (which uses the [Tinybird Events API](../ingest-apis/events-api)) and pass your own Token, which you can find in your workspace > Tokens:

```shell {% title="Endpoint URL" %}
{% user("apiHost") %}/v0/events?wait=true&name=<Data Source name>&token=<Static token>
```

{% snippet title="api-region-reminder" /%}

If you are sending a single-line JSON payload through Pubsub, select the **Enable payload unwrapping** option. This means that data isn't base64 encoded before sending it to Tinybird. If you are sending any other format through Pubsub, leave this option unselected and follow the decoding steps at the end of this guide.

Set **Retry policy** to **Retry after exponential backoff delay**. Set the **Minimum backoff** to **1** and **Maximum backoff** to **60**.


{% callout type="tip" %}
You don't need to create the data source in advance. Tinybird creates it automatically. This snippet also includes the `wait=true` parameter, which is explained in the [Events API docs](../ingest-apis/events-api#wait-for-acknowledgement).
{% /callout %}

### 3. Send sample messages

Generate and send some sample messages to test your connection. If you don't have your own messages to test, use [this script](https://gist.github.com/alejandromav/dec8e092ef62d879e6821da06f6459c2).

### 4. Check the data source

Pub/Sub starts pushing data to Tinybird. Check the Tinybird UI to confirm that the data source has been created and events are arriving.

### Decode the payload (optional)

If you selected the **Enable payload unwrapping** option, there is nothing else to do.

However, if you aren't sending single-line JSON payloads (NDJSON, JOSNL) through Pubsub, continue to base64 encode data before sending it to Tinybird. When the data arrives in Tinybird, you can decode it using the `base64Decode` function, like this:

```sql
SELECT
    message_message_id as message_id,
    message_publish_time,
    base64Decode(message_data) as message_data
  FROM events_demo
```
