gojek / courier-flutter

Dart port of our popular courier library
43 stars 8 forks source link

Add protobuf message adapter package #15

Closed alfianlosari closed 1 year ago

alfianlosari commented 1 year ago

This MR adds a package containing ProtobufMessageAdapter which depends on Google Dart Protobuf package. You can use this to listen to Protobuf based GeneratedMessage type courier stream and publish generated message as bytes.

Simply pass this to message adapters list when initializing CourierClient like so

final CourierClient courierClient = CourierClient.create(
      ....,
      messageAdapters: const <MessageAdapter>[
        ProtobufMessageAdapter(),
        JSONMessageAdapter(),
        BytesMessageAdapter(),
        StringMessageAdapter()
      ]);

Decode bytes to Pet.pb GeneratedMessage:

courierClient
        .courierMessageStream<Pet>(
            "pet/6b57d4e5-0fce-4917-b343-c8a1c77405e5/update",
            decoder: Pet.fromBuffer)
        .listen((pet) {
      print("Message received Pet: ${pet.name}");
    });

Encode Pet GeneratedMessage pb to bytes:

final pet = Pet();
    pet.name = "Hello Pet";
    courierClient.publishCourierMessage(CourierMessage(
        payload: pet,
        topic: "pet/6b57d4e5-0fce-4917-b343-c8a1c77405e5/update",
        qos: QoS.one));