Skip to content

DoNewsCode/core-pubsub

Repository files navigation

core-pubsub

A pubsub module for package Core built upon Watermill.

Build Go Reference codecov Go Report Card Sourcegraph

Example

package pubsub_test

import (
	"context"
	"fmt"
	"time"

	"github.com/DoNewsCode/core"
	pubsub "github.com/DoNewsCode/core-pubsub"
	"github.com/ThreeDotsLabs/watermill"
	"github.com/ThreeDotsLabs/watermill/message"
	"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"
)

type MyModule struct {
	goch   *gochannel.GoChannel
	cancel func()
}

func (m *MyModule) ProvidePubSub(router *message.Router) {
	router.AddNoPublisherHandler(
		"example",
		"example-in",
		m.goch,
		func(msg *message.Message) error {
			fmt.Println(string(msg.Payload))
			m.cancel()
			return nil
		},
	)
}

func Example() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	goch := gochannel.NewGoChannel(gochannel.Config{}, watermill.NopLogger{})
	module := MyModule{goch, cancel}

	c := core.Default(core.WithInline("log.level", "none"))
	c.AddModuleFunc(pubsub.New)
	c.AddModule(&module)
	go func() {
		time.Sleep(time.Second)
		goch.Publish("example-in", message.NewMessage(watermill.NewUUID(), message.Payload("foo")))
	}()
	c.Serve(ctx)

	// Output:
	// foo
}

About

A pubsub module for package core.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages