Google Cloud Pub/Sub API - 推送电子邮件 [英] Google Cloud Pub/Sub API - Push E-mail

查看:22
本文介绍了Google Cloud Pub/Sub API - 推送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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?

提前谢谢你!

推荐答案

TL;DR

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

TL;DR

Your cannot subscribe to push notifications from the client side.

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

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

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

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": "user@example.com", "historyId": "9876543210"}

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

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.

这篇关于Google Cloud Pub/Sub API - 推送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆