natsx

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2019 License: Apache-2.0 Imports: 5 Imported by: 5

README

Natswrap

natsx is a simple wrapper for nats.io client.

Usage

To start use the natsx package add import:

...
import (
  "github.com/lancer-kit/armory/natsx"
)
...
  • Fill config structure:
Field Type Required
Host string +
Port int +
User string
Password string
  • Set config:
natsx.SetCfg(cfg)

Connect will be initialized at first try to push or subscribe a message

err := PublishMessage(topic, obj)
Example
package main

import (
    "fmt"
    "github.com/lancer-kit/armory/natsx"
)

var config = natsx.Config{
    Host: "127.0.0.1",
    Port: 4222,
    User: "user",
    Password: "password",  
}

err := config.Validate()
if err != nil {
    fmt.Println('invalid nats configuration')
}

natsx.SetConfig(&config)

testMsg := []string {"1", "2"}
err := natsx.PublishMessage("Topic", testMsg)
if err != nil {
    log.Default.Error(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConn

func GetConn() (*nats.Conn, error)

func PublishMessage

func PublishMessage(topic string, msg interface{}) error

func SetConfig

func SetConfig(config *Config)

func Subscribe

func Subscribe(topic string, msgs chan *nats.Msg) (*nats.Subscription, error)

Types

type Config

type Config struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
}

Config is configuration for the interaction with the NATS server.

func (Config) ToURL

func (config Config) ToURL() string

func (Config) Validate

func (config Config) Validate() error

type Message

type Message struct {
	EventID string      `json:"eventId"`
	Result  string      `json:"result"`
	Msg     string      `json:"msg"`
	Details interface{} `json:"details"`
}

Jump to

Keyboard shortcuts

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