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

我使用node.js创build一个应用程序,每次收到电子邮件时都会从Gmail获取PUSH,然后根据CRM中的第三方数据库检查该应用程序,并在该CRM中包含电子邮件时在CRM中创build一个新字段。 我无法使用Google新的Cloud Pub / Sub,这似乎是在没有持续轮询的情况下从Gmail推送的唯一方法。

我已经通过这里的说明: https : //cloud.google.com/pubsub/prereqs,但我不明白这是应该如何从我的桌面上的应用程序工作。 看起来pub / sub可以连接到一个已validation的域,但是我不能直接连接到我的电脑上的.js脚本。 我已经保存在一个JSON文件的API密钥,并使用以下内容:

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); }); 

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

先谢谢你!

TL; DR

您无法订阅从客户端推送通知。


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

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

  // This is all the data the notifications will give you. {"emailAddress": "user@example.com", "historyId": "9876543210"} 

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