Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Pub/Sub API - Push E-mail

I'm using node.js to create an app that gets a PUSH from Gmail each time an email is received, checks it against a third party database in a CRM and creates a new field in the CRM if the e-mail is contained there. I'm having trouble using Google's new Cloud Pub/Sub, which seems to be the only way to get push from Gmail without constant polling.

I've gone through the instructions here: https://cloud.google.com/pubsub/prereqs but I don't understand how exactly this is supposed to work from an app on my desktop. It seems that pub/sub can connect to a verified domain, but I can't get it to connect directly toto the .js script that I have on my computer. I've saved the api key in a json file and use the following:

var gcloud = require('gcloud');
var pubsub;

// From Google Compute Engine:
pubsub = gcloud.pubsub({
  projectId: 'my-project',
});

// Or from elsewhere:
pubsub = gcloud.pubsub({
  projectId: 'my-project',
  keyFilename: '/path/to/keyfile.json'
});

// Create a new topic.
pubsub.createTopic('my-new-topic', function(err, topic) {});

// Reference an existing topic.
var topic = pubsub.topic('my-existing-topic');

// Publish a message to the topic.
topic.publish('New message!', function(err) {});

// Subscribe to the topic.
topic.subscribe('new-subscription', function(err, subscription) {
  // Register listeners to start pulling for messages.
  function onError(err) {}
  function onMessage(message) {}
  subscription.on('error', onError);
  subscription.on('message', onMessage);

  // Remove listeners to stop pulling for messages.
  subscription.removeListener('message', onMessage);
  subscription.removeListener('error', onError);
});

However, I get errors as if it isn't connecting to server and on the API list I see only errors, no actual successes. I'm clearly doing something wrong, any idea what it might be?

Thank you in advance!

like image 368
Sekoul Avatar asked Jul 23 '15 22:07

Sekoul


1 Answers

TL;DR

Your cannot subscribe to push notifications from the client side.


Set up an HTTPS server to handle the messages. Messages will be sent to the URL endpoint that you configure, representing that server's location. Your server must be reachable via a DNS name and must present a signed SSL certificate. (App Engine applications are preconfigured with SSL certificates.)

Just subscribe to the push notifications on your server, and when you get the notification, you can figure out who it concerns. The data you will get from the notifications is what user that it concerns, and the relevant historyId, like so:

 // This is all the data the notifications will give you.
 {"emailAddress": "[email protected]", "historyId": "9876543210"}

Then you could e.g. emit an event through Socket.io to the relevant user if he is online, and have him do a sync with the supplied historyId on the client side.

like image 119
Tholle Avatar answered Sep 18 '22 14:09

Tholle