From 2528b254570c538a79ed077e0e3fec8068933ce1 Mon Sep 17 00:00:00 2001 From: Michael Collins Date: Sun, 8 Dec 2024 20:52:38 -0700 Subject: [PATCH] Update go-service documentation I added examples of how to use the TopicEventSubscriber interface to create a new subscriber for both HTTP and gRPC services. Signed-off-by: Michael Collins --- .../en/go-sdk-docs/go-service/grpc-service.md | 29 +++++++++++++++++++ .../en/go-sdk-docs/go-service/http-service.md | 29 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/daprdocs/content/en/go-sdk-docs/go-service/grpc-service.md b/daprdocs/content/en/go-sdk-docs/go-service/grpc-service.md index 4ac37c39..33c7909d 100644 --- a/daprdocs/content/en/go-sdk-docs/go-service/grpc-service.md +++ b/daprdocs/content/en/go-sdk-docs/go-service/grpc-service.md @@ -83,6 +83,35 @@ if err != nil { } ``` +You can also create a custom type that implements the `TopicEventSubscriber` interface to handle your events: + +```go +type EventHandler struct { + // any data or references that your event handler needs. +} + +func (h *EventHandler) Handle(ctx context.Context, e *common.TopicEvent) (retry bool, err error) { + log.Printf("event - PubsubName:%s, Topic:%s, ID:%s, Data: %v", e.PubsubName, e.Topic, e.ID, e.Data) + // do something with the event + return true, nil +} +``` + +The `EventHandler` can then be added using the `AddTopicEventSubscriber` method: + +```go +sub := &common.Subscription{ + PubsubName: "messages", + Topic: "topic1", +} +eventHandler := &EventHandler{ +// initialize any fields +} +if err := s.AddTopicEventSubscriber(sub, eventHandler); err != nil { + log.Fatalf("error adding topic subscription: %v", err) +} +``` + ### Service Invocation Handler To handle service invocations you will need to add at least one service invocation handler before starting the service: diff --git a/daprdocs/content/en/go-sdk-docs/go-service/http-service.md b/daprdocs/content/en/go-sdk-docs/go-service/http-service.md index 643f24f2..c73487e0 100644 --- a/daprdocs/content/en/go-sdk-docs/go-service/http-service.md +++ b/daprdocs/content/en/go-sdk-docs/go-service/http-service.md @@ -78,6 +78,35 @@ if err != nil { } ``` +You can also create a custom type that implements the `TopicEventSubscriber` interface to handle your events: + +```go +type EventHandler struct { + // any data or references that your event handler needs. +} + +func (h *EventHandler) Handle(ctx context.Context, e *common.TopicEvent) (retry bool, err error) { + log.Printf("event - PubsubName:%s, Topic:%s, ID:%s, Data: %v", e.PubsubName, e.Topic, e.ID, e.Data) + // do something with the event + return true, nil +} +``` + +The `EventHandler` can then be added using the `AddTopicEventSubscriber` method: + +```go +sub := &common.Subscription{ + PubsubName: "messages", + Topic: "topic1", +} +eventHandler := &EventHandler{ +// initialize any fields +} +if err := s.AddTopicEventSubscriber(sub, eventHandler); err != nil { + log.Fatalf("error adding topic subscription: %v", err) +} +``` + ### Service Invocation Handler To handle service invocations you will need to add at least one service invocation handler before starting the service: