> For the complete documentation index, see [llms.txt](https://docs.sprinthub.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sprinthub.com/en/topics/integrations/integration-google-ads-and-gmail/connect-gmail-in-real-time-pub-sub.md).

# Connect Gmail in real time (Pub/Sub)

This guide shows, **screen by screen**, how to configure Google Cloud Pub/Sub so SprintHub can receive Gmail emails in **real time** in SAC360 (seconds), instead of waiting for periodic synchronization via IMAP (1–5 min).

> The screens were captured by going through the real process from scratch (new project). The project/topic/account names used here are examples — replace them with your own.

**Summary of what will be created in Google Cloud:**

| Resource            | Example in this guide                                                  |
| ------------------- | ---------------------------------------------------------------------- |
| Project             | `sprinthub-gmail-pubsub`                                               |
| Service account     | `sprinthub-pubsub-push@sprinthub-gmail-pubsub.iam.gserviceaccount.com` |
| Pub/Sub topic       | `projects/sprinthub-gmail-pubsub/topics/gmail-notifications`           |
| Subscription (push) | `gmail-notifications-sub`                                              |

In the end, two values are taken to SprintHub: the **topic name** and the **webhook URL** (this last one is already provided on the integration configuration screen).

***

### Prerequisites

* Google account with access to the [Google Cloud Console](https://console.cloud.google.com/).
* Credit card registered with Google Cloud (Google requirement; usage stays within the free tier — in practice, free).
* The **webhook URL** of your SprintHub instance, which appears on the Gmail integration configuration screen itself (something like `https://sprinthub-api-XXXX.sprinthub.app/email/webhook/gmail`). The path `/email/webhook/gmail` is fixed.

***

### Step 1 — Access the console and sign in

Access <https://console.cloud.google.com/> and sign in with your Google account.

<figure><img src="/files/c872a28bffe8e66600b868020250dfd1c76d9e68" alt=""><figcaption></figcaption></figure>

***

### Step 2 — Create (or select) the project

1. Click on the **project selector** at the top (next to "Google Cloud").

<figure><img src="/files/f5ac8ad45f8c88e2b43a374d0d93524a24d9cb1b" alt=""><figcaption></figcaption></figure>

2. Click on **New project**, give it a name (e.g., `sprinthub-gmail-pubsub`) and click **Create**.

<figure><img src="/files/78b1cf1af04be1c4ffab6b2ae6dd98fc53097b19" alt=""><figcaption></figcaption></figure>

3. Wait for creation (\~30s) and **select the newly created project** in the project selector.

> A newly created project may take a few seconds before access is granted (IAM). If "You need additional access" appears, wait a moment and reload.

***

### Step 3 — Create the service account

1. In **search bar** at the top, type `service accounts` and click the result **Service accounts** (IAM and admin).

<figure><img src="/files/d6fff24a964a71f4c8817f1057eb0d9fc724c46e" alt=""><figcaption></figcaption></figure>

2. On the Service accounts page, click **Create service account**.

<figure><img src="/files/67fcf20daa16654798a5193ad8d7b4a8b82aa6f6" alt=""><figcaption></figcaption></figure>

3. Fill in the **Service account name** (e.g.: `sprinthub-pubsub-push`) — the ID and email are generated automatically. The permission steps are optional.

<figure><img src="/files/c3c7dd11618e6148a52e684cd9a1b9b0a369cbc9" alt=""><figcaption></figcaption></figure>

4. Click on **Create and close**. The account appears in the list.

<figure><img src="/files/6761a65f40249b6ce4e1266cd53e0b10c954f93f" alt=""><figcaption></figcaption></figure>

> Save the service account email — it will be selected in the push subscription (Step 6). E.g.: `sprinthub-pubsub-push@sprinthub-gmail-pubsub.iam.gserviceaccount.com`.

***

### Step 4 — Create the Pub/Sub topic

1. In **search bar**, type `pub/sub` and click the product **Pub/Sub**.

<figure><img src="/files/244867977653afbc3b541ff2daa79ed31a337e93" alt=""><figcaption></figcaption></figure>

> On the first visit, the Cloud Pub/Sub API is automatically enabled for the project.

2. In the Topics list, click **Create topic**.

<figure><img src="/files/900598a1dd1c6f32d7f5e782cb5c21bd30a0ea2b" alt=""><figcaption></figcaption></figure>

3. Define the **Topic ID** (e.g.: `gmail-notifications`). **No need to change anything else** — leave the checkbox "Add a default subscription" checked (it creates the subscription we'll edit in Step 6). Click **Create**.

<figure><img src="/files/e2298c969de5e6e6e57e946c48cbd0ee54610838" alt=""><figcaption></figcaption></figure>

4. The topic is created with a default subscription (`gmail-notifications-sub`).

<figure><img src="/files/173192bac939c3ecd0190e862b695e896bc1de03" alt=""><figcaption></figcaption></figure>

***

### Step 5 — Authorize Gmail to publish to the topic

Gmail publishes notifications to the topic using a fixed Google service agent. You need to grant it permission.

1. Still on the topic page, in the permissions panel on the right, click **Add principal**.
2. In **New principals**, paste exactly:

   ```
   gmail-api-push@system.gserviceaccount.com
   ```
3. In **Assign roles**, select the role **Editor** (if needed, search for "editor" in the role selector).

<figure><img src="/files/a4cd1db73cd31dfb9ab90e711ea450ee2bf72f56" alt=""><figcaption></figcaption></figure>

4. Click on **Save**.

<figure><img src="/files/3bcf6b9b47735fe7d1ad092a274a0cc8924fd67c" alt=""><figcaption></figcaption></figure>

> Without this step, when enabling real time the error appears `User not authorized to publish to topic`.

***

### Step 6 — Configure the subscription as Push

The subscription is what turns each topic message into an HTTP call to SprintHub. We are going to **edit the default subscription** created along with the topic (if it doesn't exist, click **Create subscription** on the topic).

1. Open the subscription `gmail-notifications-sub` and click on **Edit**.
2. In **Delivery type**, select **Push**.
3. In **Endpoint URL**, paste the webhook URL shown on the integration configuration screen in SprintHub:

   ```
   https://sprinthub-api-XXXX.sprinthub.app/email/webhook/gmail
   ```
4. Check **Enable authentication** and, in **Service account**, select the account created in Step 3 (`sprinthub-pubsub-push@...`). The field **Audience** can be left blank.

<figure><img src="/files/45223a4d3b75198b941995ebfcb49a64c963a655" alt=""><figcaption></figcaption></figure>

5. Click on **Refresh**.

<figure><img src="/files/462882c777035c7eaa0db1dd180c84994a3c51a9" alt=""><figcaption></figcaption></figure>

> The URL must be **valid HTTPS**. Since we use service account authentication, there is no need to verify the domain in Google.

***

### Step 7 — Copy the topic name

Go back to the topic and copy the **Topic name** in full (copy button next to it):

<pre><code><strong>projects/sprinthub-gmail-pubsub/topics/gmail-notifications
</strong></code></pre>

<figure><img src="/files/0c2271559cc17cd985bdc1cf86bc7387b51c91e4" alt=""><figcaption></figcaption></figure>

***

### Step 8 — Paste it into SprintHub and enable real time

In SprintHub, go to **Settings → Integrations → Gmail account → SAC 360**. The real-time configuration is in the section **"Real-time reception (Pub/Sub)"**, at the end of the SAC 360 card (it only appears with "Create tickets automatically" enabled):

1. **Copy the webhook URL** shown there (copy button) — it is the same one used in Step 6.
2. Paste the topic name in the field **Google Pub/Sub topic**.
3. Turn on the toggle **Receive in real time (Pub/Sub)**.

> Screen notes:
>
> * The toggle **only enables** with a valid Pub/Sub Topic format (`projects/<project>/topics/<topic>`).
> * When **saving the integration without a valid topic**, real time is **automatically disabled** (prevents an inconsistent state).

Done — from here on, every new email in the Gmail account generates a ticket/message in SAC360 in seconds, without waiting for IMAP synchronization.

***

### How to verify that it is working

1. Send a test email to the connected Gmail account.
2. In a few seconds, the ticket/message should appear in SAC360 (without refreshing).
3. In the Google Cloud Console: **Pub/Sub → Subscriptions → `gmail-notifications-sub` → Metrics** should show "Published messages" and "Delivered messages" increasing.

To debug from the server side, the backend logs distinguish the arrival path (filter by `email_attendance`):

```
[EmailAttendance] ⇢ ENTRY source=pubsub topic=projects/.../topics/gmail-notifications account=... messageId=...
[EmailAttendance] ⇢ ENTRY source=imap  account=... messageId=...
```

`source=pubsub` = arrived in real time; `source=imap` = arrived via periodic synchronization.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.sprinthub.com/en/topics/integrations/integration-google-ads-and-gmail/connect-gmail-in-real-time-pub-sub.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
