Google Cloud Pub / Sub API - 推送电子邮件

时间:2022-08-07 15:26:40

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.

我正在使用node.js创建一个应用程序,每次收到电子邮件时都会从Gmail获取PUSH,将其与CRM中的第三方数据库进行核对,如果电子邮件包含在CRM中,则在CRM中创建新字段。我在使用谷歌新的Cloud Pub / Sub时遇到了麻烦,这似乎是在没有持续轮询的情况下从Gmail推送的唯一方法。

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:

我已经完成了这里的说明:https://cloud.google.com/pubsub/prereqs但我不明白这应该是从桌面上的应用程序中运行的。似乎pub / sub可以连接到经过验证的域,但我无法直接连接到我的计算机上的.js脚本。我已将api密钥保存在json文件中并使用以下内容:

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?

但是,我得到错误,好像它没有连接到服务器,在API列表上我只看到错误,没有实际的成功。我显然做错了什么,不知道它可能是什么?

Thank you in advance!

先谢谢你!

1 个解决方案

#1


4  

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.)

设置HTTPS服务器以处理消息。消息将发送到您配置的URL端点,表示该服务器的位置。您的服务器必须可通过DNS名称访问,并且必须提供已签名的SSL证书。 (App Engine应用程序预先配置了SSL证书。)

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:

只需在您的服务器上订阅推送通知,当您收到通知时,您可以找出它所关注的问题。您将从通知中获得的数据是它所关注的用户以及相关的historyId,如下所示:

 // This is all the data the notifications will give you.
 {"emailAddress": "user@example.com", "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.

然后你可以,例如如果他在线,则通过Socket.io向相关用户发出事件,并让他与客户端提供的historyId进行同步。

#1


4  

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.)

设置HTTPS服务器以处理消息。消息将发送到您配置的URL端点,表示该服务器的位置。您的服务器必须可通过DNS名称访问,并且必须提供已签名的SSL证书。 (App Engine应用程序预先配置了SSL证书。)

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:

只需在您的服务器上订阅推送通知,当您收到通知时,您可以找出它所关注的问题。您将从通知中获得的数据是它所关注的用户以及相关的historyId,如下所示:

 // This is all the data the notifications will give you.
 {"emailAddress": "user@example.com", "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.

然后你可以,例如如果他在线,则通过Socket.io向相关用户发出事件,并让他与客户端提供的historyId进行同步。