Flutter Cloud Pub Sub - How to create a StreamingPull that will continuously listen for messages?

Viewed 366

I have an API that publishes messages to subscribers.

In flutter, I have a subscriber and I need to pull messages from the API every time they are published.

 String subscriptionName = 'projects/test/subscriptions/test-sub';
    pubSubClient.projects.subscriptions
        .pull(
            PullRequest.fromJson({
              "maxMessages": 1000,
            }),
            subscriptionName)
        .then((pullResponse) {
      if (pullResponse.receivedMessages != null &&
          pullResponse.receivedMessages.isNotEmpty) {
        List<String> ids = [];
        pullResponse.receivedMessages.forEach((element) {
          ids.add(element.ackId);
        });
        pubSubClient.projects.subscriptions.acknowledge(
            AcknowledgeRequest.fromJson({"ackIds": ids}), subscriptionName);
      }
    });

This works but this is run only once. In the Google Cloud API, it says to use a StreamingPull to efficiently receive messages?

How can this be done in flutter and what other options are there?

1 Answers

Unfortunately, this was not possible.

I decided to change the implementation and not use Pub-Sub.

The Google developer said

Sadly, you are NOT able to do a "StreamingPull" with the REST API – which is what this is.

Related