Skip to content

Commit

Permalink
Update go-service documentation
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mfcollins3 committed Dec 9, 2024
1 parent d310732 commit 2528b25
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions daprdocs/content/en/go-sdk-docs/go-service/grpc-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
29 changes: 29 additions & 0 deletions daprdocs/content/en/go-sdk-docs/go-service/http-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down

0 comments on commit 2528b25

Please sign in to comment.