在 Google PubSub Pull 中收到订阅者数据后回复消息

Reply a message after receiving data on subscriber in Google PubSub Pull

是否可以在收到来自 Publisher 的数据后回复消息。 一旦发布者发布消息,它必须是直接回复。 我正在使用 Google PubSub 服务。

https://cloud.google.com/pubsub/docs/pull

Publisher/Sender (PHP):

$sendToOps =[];
$sendToOps['MESSAGE'] = "my message";
$topicName = env('GOOGLE_CLOUD_TO_OPS_TOPIC');

$pubSub = new PubSubClient();
$topic = $pubSub->topic($topicName);
$ret =  $topic->publish([
          'attributes'=>$sendToOps
       ]);

//**********The word "Apple" must output here**********
echo $ret;
//*****************************************************

Subscriber/Receiver (Javascript):

'use strict';


//Get .env File Data
require('dotenv').config({path: '/usr/share/nginx/html/myProject/.env'});
var request = require('request');

var port = process.env.PORT_GATEWAY;
var host = process.env.IP_PUSH;
var test = process.env.TEST_FLAG;
var pubsubSubscription = process.env.GOOGLE_CLOUD_TO_OPS_SUBSCRIPTION;
const keyFilePath= 'public/key/key.json';

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub({
  keyFilename: keyFilePath
});

function listenForMessages() {
  // References an existing subscription
  const subscription = pubSubClient.subscription(pubsubSubscription);

  // Create an event handler to handle messages
  const messageHandler = message => {
  console.log(message.attributes);


   //*****************************************************
   //I want to reply to Sender with the word "Apple" here
   //*****************************************************


   message.ack()
  };
   
  subscription.on('message', messageHandler);
}

  listenForMessages();


Is it possible to reply a message once you received a data from Publisher.

取决于你所说的“回复”是什么意思。消息的发布者在 Pub/Sub 主题上发布消息。订阅者从 Pub/Sub 订阅接收消息。这里没有双向通信通道。没有 Pub/Sub 回复方法。

订阅者可以将消息发布到发布者作为订阅者阅读的不同主题。双方都是发布者和订阅者,但主题不同。

收到消息后,订阅者可以直接调用发布者的 API。

但是,Publish/Subscribe 的目的是将发送方与接收方分离,而不是将它们锁定在一起。