Skip to content

Commit

Permalink
tested examples for channel and redis pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
John Deng committed Aug 20, 2024
1 parent 5ec50e7 commit c3ab047
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# hiboot-messaging

22 changes: 15 additions & 7 deletions examples/channel-pubsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@ package main

import (
"context"
"encoding/json"
"github.com/hidevopsio/hiboot-messaging/pubsub"
"github.com/hidevopsio/hiboot/pkg/log"
"github.com/hidevopsio/hiboot/pkg/utils/idgen"
"hiboot-messaging/pubsub"
"time"
)

type FooReceivedEvent struct {
ID string

ID string
Name string
}

func (e *FooReceivedEvent) MarshalBinary() ([]byte, error) {
return json.Marshal(e)
}

func (e *FooReceivedEvent) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, e)
}

func main() {
// Example of creating a new Redis client and RedisPubSub instance

chnPubSub := pubsub.NewChannelPubSub[FooReceivedEvent]()
fooReceivedEventPubSub := pubsub.NewChannelPubSub[*FooReceivedEvent]()
ctx := context.Background()
ch, err := chnPubSub.Subscribe(ctx, "foo")
ch, err := fooReceivedEventPubSub.Subscribe(ctx, "foo")
if err != nil {
log.Errorf("Error subscribing: %v", err)
return
Expand All @@ -31,11 +39,11 @@ func main() {
time.Sleep(5 * time.Second)
var id string
id, err = idgen.NextString()
event := FooReceivedEvent{
event := &FooReceivedEvent{
ID: id,
Name: "Test Event",
}
err = chnPubSub.Publish(ctx, "foo", event)
err = fooReceivedEventPubSub.Publish(ctx, "foo", event)
if err != nil {
log.Error(err)
}
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace (
github.com/hidevopsio/hiboot-data => ../hiboot-data
)
//
//replace (
// github.com/hidevopsio/hiboot-data => ../hiboot-data
//)

0 comments on commit c3ab047

Please sign in to comment.