nats

package
v0.1.25 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 1 Imported by: 0

README

Ssi Core / Nats

This package includes the implementation of JetStream by nats for microservices to communicate with each other.

Types

connection
type Conn struct {
	C  *nats.Conn
	JS nats.JetStreamContext
}

Conn is a struct that includes the connection and the JetStream context.

#### functions

func (c *Conn) Close()
func (c *Conn) Publish(subject string, data []byte) (*nats.PubAck, error)
func (c *Conn) Subscribe(subject string, cb nats.MsgHandler) (*nats.Subscription, error)
func (c *Conn) AddStream(name string) (*nats.StreamInfo, error)
func (c *Conn) UpdateStream(name string) (*nats.StreamInfo, error)

Create a Connection

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/nats"
    "fmt"
)

func main() {
    n := nats.New()
    fmt.Println(n.C.ConnectedUrl())
}

Close a Connection

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/nats"
    "fmt"
)

func main() {
    n := nats.New()
    n.Close()
    fmt.Println(n.C.ConnectedUrl())
}

Publish a Message

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/nats"
    "fmt"
)

func main() {
    n := nats.New()
    _, err := n.Publish("test", []byte("Hello, World!"))
    if err != nil {
        fmt.Println(err)
    }
}

Subscribe a Message

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/nats"
	natsGo "github.com/nats-io/nats.go"
    "fmt"
)

func main() {
    n := nats.New()
    _, err := n.Subscribe("test", func(msg *natsGo.Msg) {
        fmt.Println(string(msg.Data))
    })
    if err != nil {
        fmt.Println(err)
    }
}

Add a Stream

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/nats"
    "fmt"
)

func main() {
    n := nats.New()
    _, err := n.AddStream("test")
    if err != nil {
        fmt.Println(err)
    }
}

Update a Stream

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/nats"
    "fmt"
)

func main() {
    n := nats.New()
    _, err := n.UpdateStream("test")
    if err != nil {
        fmt.Println(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	C  *nats.Conn
	JS nats.JetStreamContext
}

func New

func New() *Conn

func (*Conn) AddStream

func (c *Conn) AddStream(name string) (*nats.StreamInfo, error)

func (*Conn) Close

func (c *Conn) Close()

func (*Conn) Publish

func (c *Conn) Publish(subject string, data []byte) (*nats.PubAck, error)

func (*Conn) Subscribe

func (c *Conn) Subscribe(subject string, cb nats.MsgHandler) (*nats.Subscription, error)

func (*Conn) UpdateStream

func (c *Conn) UpdateStream(name string) (*nats.StreamInfo, error)

Jump to

Keyboard shortcuts

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