Google Pub/Sub for message sync
You can use Google Pub/Sub to sync Gmail messages between Google and Nylas in real-time. This guide explains how to set up PubSub for your project.
Automatic set up with a script
To simplify installation, Nylas maintains a script that you can run to automatically provision the GCP resources in Golang.
Prerequisites
- Install the Go language.
- Install the
gcloud
CLI tool. - Make sure that you have an existing Google application connected to your Nylas application.
Your Pub/Sub Topic and related resources MUST be set up in the same Google app you use to authenticate accounts with Nylas.
-
Make sure that you have the PubSub and IAM APIs enabled in your GCP project. You can do this using the
gcloud
CLI tool:gcloud service enable pubsub.googleapis.com
gcloud service enable iam.googleapis.com
Running the script
-
Pull down the script from the nylas-samples/infra-setup repository, and change your directory to
google-pubsub-sync
.git clone https://github.com/nylas-samples/infra-setup
cd infra-setup/google-pubsub-sync -
Switch the project setup to your GCP project.
gcloud config set project $YOUR_GOOGLE_PROJECT_ID
-
Authenticate with GCP.
Make sure that the account you authenticate with has permission to create PubSub and IAM resources.
gcloud auth login
gcloud auth application-default login -
Fetch the dependencies for the script.
go get .
-
Execute the script.
go run main.go --projectId $YOUR_GCP_PROJECT_ID
- To setup in environments other than US, use the
--env
flag. Supported values areus
,eu
andstaging
.
go run main.go --projectId $YOUR_GCP_PROJECT_ID --env eu
- If the script fails with a 403 error with a message of
SERVICE_DISABLED
, make sure to enabled both the IAM and PubSub API in your project:
gcloud services enable pubsub.googleapis.com
gcloud services enable iam.googleapis.com - To setup in environments other than US, use the
-
Add the topic to the Nylas Dashboard.
The topic name should be
projects/$YOUR_GOOGLE_PROJECT_NAME/topics/nylas-gmail-realtime
.
Manual set up
Prerequisites
- Make sure that you have an existing Google application connected to your Nylas application.
Your Pub/Sub Topic and related resources MUST be set up in the same Google app you use to authenticate accounts with Nylas.
Create a Service Account
-
Open Service Accounts in the Google Cloud Platform console.
-
Select your project and click Create Service Account.
-
Name the account
nylas-gmail-realtime
.The name must be exactly
nylas-gmail-realtime
for the integration to work. -
Leave the Grant this service account access to project and Grant users access to this service account fields blank.
-
Click Done.
Create a Pub/Sub topic
Your Pub/Sub Topic and related resources MUST be set up in the same Google app you use to authenticate accounts with Nylas.
-
Search for "pub/sub" in the search bar and select Pub/Sub.
-
Click CREATE TOPIC.
-
Enter
nylas-gmail-realtime
for the topic ID and leave everything else as is. The topic ID listed is required for the integration to work. -
On the next page, if it's not already open, click SHOW INFO PANEL.
-
In the panel, click ADD PRINCIPAL.
-
Enter
[email protected]
in the New principals field. -
Set the role to Pub/Sub Publisher.
-
On the Topics page, find the Subscriptions section and click
nylas-gmail-realtime-sub
. -
Select the subscription and click EDIT.
-
Change the Delivery type to Push.
-
Fill in the Endpoint URL field:
- for the US, use
https://gmailrealtime.us.nylas.com
. - for the EU, use
https://gmailrealtime.eu.nylas.com
.
- for the US, use
-
Select Enable Authentication and choose the service account you made in create a service account.
If you're setting up multiple regions in a single GCP application, you'll need to create a subscription for each region.
-
When prompted, grant the
roles/iam.serviceAccountTokenCreator
to your service account. If the prompt doesn't appear, follow the steps below to add the role to your service account:- From the main menu, navigate to IAM & Admin > Service Accounts
- Copy the full service account email for the service account you created in create a service account (it should start with
nylas-gmail-realtime
). - Select the service account (it should be called
nylas-gmail-realtime
). - Navigate to the Permissions tab and make sure you're on the Principals tab at the bottom.
- If the service account
nylas-gmail-realtime-email
isn't listed under principals, click Grant Access and paste the email into the New Principals field.- If it's listed, click the Edit icon next to that principal.
- On the pop-up, click ADD ANOTHER ROLE.
- Search for
service account token creator
. - Select the role.
- Click SAVE.
-
Under Expiration period, select Never expire
-
Leave the other fields as they are and click Update. The screen saves your changes and returns to the Subscription page.
-
Copy the Topic Name for the next step.
The topic name must be nylas-gmail-realtime
and the principal must use the email [email protected]
for Google Pub/Sub to work.
Add the Topic name to your dashboard
Before adding the Topic name, you need to enable Google Pub/Sub for your Nylas application.
- In the Nylas Dashboard, click App Settings.
- Click Authentication.
- In the Google Auth section, enter the topic you created in Create a Pub/Sub Topic in the GCP Topic Name field. For example,
projects/nylas-test/topics/nylas-gmail-realtime
. - Repeat steps one to three for each Nylas app that needs real-time Gmail message sync.
Tip!: You can use the same Pub/Sub topic for all of your Nylas apps.