jetstreamclient

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2022 License: GPL-2.0 Imports: 8 Imported by: 1

README

Softcom Jetstream EventStore Client - Go

How to Publish
// Publish
package main

import (
	"encoding/json"
	"fmt"
	"github.com/softcomoss/jetstreamclient/options"
	jetstream "github.com/softcomoss/jetstreamclient/jsm"
	"log"
	"time"
)

func main() {
	ev, err := jetstream.Init(options.Options{
		ServiceName: "USERS",
		Address:     "localhost:4222",
	})
	if err != nil {
		log.Fatal(err)
	}

	start := time.Now()
	const topic = "test"

	data, err := json.Marshal(struct {
		FirstName string `json:"first_name"`
	}{
		FirstName: "Justice Nefe",
	})

	if err != nil {
		panic(err)
	}

	if err := ev.Publish(topic, data); err != nil {
		fmt.Print(err, " Error publishing.\n")
	}

	end := time.Now()

	diff := end.Sub(start)

	fmt.Printf("Start: %s, End: %s, Diff: %s", start, end, diff)
}

How to Subscribe
// Subscribe
package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"github.com/softcomoss/jetstreamclient"
	jetstream "github.com/softcomoss/jetstreamclient/jsm"
	"github.com/softcomoss/jetstreamclient/options"

	"log"
	"time"
)

func main() {

	name := flag.String("name", "", "help message for flagname")
	flag.Parse()

	ev, err := jetstream.Init(options.Options{
		ServiceName: *name,
		Address:     "localhost:4222",
	})

	if err != nil {
		log.Fatal(err)
	}

	handleSubEv := func() error {
		const topic = "test"
		return ev.Subscribe(topic, func(event jetstreamclient.Event) {
			defer event.Ack()
			var pl struct {
				FirstName string `json:"first_name"`
			}
			
			if err := json.Unmarshal(event.Data(), &pl); err != nil {
				fmt.Print(err, " Err parsing event into pl.")
				return
			}

		}, options.NewSubscriptionOptions().SetSubscriptionType(options.Shared))
	}

	// You can use context.WithCancel()
	ev.Run(context.Background(), handleSubEv)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyStoreName          = errors.New("Sorry, you must provide a valid store name")
	ErrInvalidURL              = errors.New("Sorry, you must provide a valid store URL")
	ErrInvalidTlsConfiguration = errors.New("Sorry, you have provided an invalid tls configuration")
	ErrCloseConn               = errors.New("connection closed")
)

Functions

func GenerateRandomString

func GenerateRandomString() string

Types

type Event

type Event interface {
	Ack()
	Data() []byte
	Topic() string
}

type EventHandler

type EventHandler func() error

func (EventHandler) Run

func (f EventHandler) Run()

type EventStore

type EventStore interface {
	Publish(topic string, message []byte) error
	Subscribe(topic string, handler SubscriptionHandler, opts ...*options.SubscriptionOptions) error
	GetServiceName() string
	Run(ctx context.Context, handlers ...EventHandler)
}

type ReplyHandler

type ReplyHandler func(input []byte) ([]byte, error)

type SubscriptionHandler

type SubscriptionHandler func(event Event)

Directories

Path Synopsis
_lab
pub
sub

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL