9

我正在使用 node.js 创建一个应用程序,该应用程序每次收到电子邮件时都会从 Gmail 获取 PUSH,将其与 CRM 中的第三方数据库进行检查,并在 CRM 中创建一个新字段(如果电子邮件包含在其中) . 我在使用 Google 的新 Cloud Pub/Sub 时遇到了问题,这似乎是无需持续轮询即可从 Gmail 获取推送的唯一方法。

我已经阅读了这里的说明: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);
});

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

先感谢您!

4

1 回答 1

4

TL;博士

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


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

只需订阅您服务器上的推送通知,当您收到通知时,您就可以弄清楚它与谁有关。您将从通知中获得的数据是它所关注的用户以及相关的 historyId,如下所示:

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

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

于 2015-07-23T23:00:21.777 回答