Publish a pubsub message in flutter/dart

3,004

Ok, for those who wonder, here is what I have come up with:

var messages = {
    'messages': [
                  {
                    'data': base64Encode(utf8.encode('{"foo": "bar"}')),
                  },
                ]
              };

pubSubClient.projects.topics
                  .publish(new PublishRequest.fromJson(messages), "your topic")
                  .then((publishResponse) {
                debugPrint(publishResponse.toString());
              }).catchError((e,m){
                debugPrint(e.toString());
              });

Maybe someone should write an article on it, this was not very clear and I had to try different things and also read the source from the pubsub client library...

Share:
3,004
GwydionFR
Author by

GwydionFR

Updated on December 06, 2022

Comments

  • GwydionFR
    GwydionFR over 1 year

    I'm trying to publish a pub/sub message in my flutter application like this:

    import 'package:flutter/material.dart';
    import 'package:googleapis/pubsub//v1.dart';
    import 'package:googleapis_auth/auth_io.dart';
    
    const _SCOPES = const [PubsubApi.PubsubScope];
    
    class Activities extends StatefulWidget {
      Activities();
    
      @override
      _Activities createState() => _Activities();
    }
    
    final _credentials = new ServiceAccountCredentials.fromJson(r'''
    {
      "type": "service_account",
      ...
    }
    ''');
    
    class _Activities extends State<Activities> with SingleTickerProviderStateMixin {
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: Center(
            child: Container(
              child: new MaterialButton(
                onPressed: () {
                  debugPrint("trying to publish a message...");
                  clientViaServiceAccount(_credentials, _SCOPES)
                    .then((http_client) {
                      var pubSubClient = new PubsubApi(http_client);
                      Map<String, dynamic> message = new Map<String, dynamic>();
                      message["data"] = Map<String, dynamic>();
                      message["data"]["type"] = "foo";
                      message["data"]["senderId"] = "bar";
    
                      pubSubClient.projects.topics
                          .publish(new PublishRequest.fromJson(message), "projects/myProject/topics/events")
                          .then((publishResponse) {
                        debugPrint(publishResponse.toString());
                      }).catchError((e,m){
                        debugPrint(e.toString());
                      });
                    })
                    .catchError((e,m) {
                      debugPrint(e.toString());
                    });
                },
                child: new Text("Publish message"),
              ),
            ),
          )
        );
      }
    }
    

    But in the logs I get the following error message:

    I/flutter ( 5281): DetailedApiRequestError(status: 400, message: The value for 0 is too small. You passed message_count in the request, but the minimum value is 1.)

    I googled this message but did not found anything. I think my message structure is maybe wrong, but maybe it's something else ? Don't wanna lose too much time on it...