natsws

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2023 License: Apache-2.0 Imports: 17 Imported by: 8

README

natsws

A go-app component and proxy to allow nats client connections from a go-app application.

This was written for a very specific use case to connect to the same host:port as the go-app backend.

Don't use this if you need:

  • Fail-over requirements provided by the default client.
  • Support for jwt tokens or other security in the default client.

This repository contains an example go-app application at goapp to demonstrate how to use the component and the proxy.

See root.go for component usage, demo.go for interacting with nats, and service.go for configuration of the proxy.

To run the demo application, change the working directory to goapp-natsws/internal and issue a make command to build and run the demo application. By default, the application will run on port 8080 which can be changed in the Makefile.

The first release used nats.InProcessServer to make the connection to the websocket proxy which is still the default.

If the environment UseDialer is set, a nats.CustomDialer will be used instead. This environment must also be present in the app.Handler environment for the client to pick it up.

If the backend urls returned from Manager begin with http or https, then the Proxy will use httputil.ReverseProxy and the websocket handshake will occur in the nats codebase.

Go Report Card

created by tigwen

Documentation

Index

Examples

Constants

View Source
const Ping = State + ".ping"
View Source
const State = "natsws.Connection"
View Source
const StateClientName = State + ".clientName"
View Source
const UseDialer = "GOAPP_NATSWS_DIALER"

Variables

This section is empty.

Functions

func Observe

func Observe(ctx app.Context, value *Connection) app.Observer

Observe simplifies observing the State of the Connection.

	The pointer to the Connection must not be nil.
 See example for ways to initialize the Connection pointer.
Example
package main

import "github.com/maxence-charriere/go-app/v9/pkg/app"

type Demo struct {
	app.Compo
	conn Connection
}

// OnMount demonstrating non pointer Connection.
func (d *Demo) OnMount(ctx app.Context) {
	Observe(ctx, &d.conn).OnChange(func() {
		if d.conn.ChangeReason() == Connect {
			// first time setup
		} else {
			// reconnect setup
		}
	})
}

type DemoTwo struct {
	app.Compo
	conn *Connection
}

// OnMount demonstrating pointer Connection.
func (d *DemoTwo) OnMount(ctx app.Context) {
	d.conn = &Connection{}
	Observe(ctx, d.conn)
}

func main() {
	// the two components above show two different methods
	// for calling Observe with a non nil pointer Connection
}
Output:

Types

type ChangeReason added in v1.0.1

type ChangeReason string
const Connect ChangeReason = "connect"
const Disconnect ChangeReason = "disconnect"
const Reconnect ChangeReason = "reconnect"

type Component

type Component struct {
	app.Compo
	// contains filtered or unexported fields
}

func (*Component) OnMount

func (n *Component) OnMount(ctx app.Context)

func (*Component) Render

func (n *Component) Render() app.UI

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

func (*Connection) ChangeReason added in v1.0.1

func (c *Connection) ChangeReason() ChangeReason

func (*Connection) ClientName

func (c *Connection) ClientName() string

func (*Connection) InProcessConn

func (c *Connection) InProcessConn() (netConn net.Conn, err error)

func (*Connection) IsConnected

func (c *Connection) IsConnected() bool

func (*Connection) Nats

func (c *Connection) Nats() (conn *nats.Conn, err error)

func (*Connection) Publish

func (c *Connection) Publish(subject string, message []byte) (err error)

func (*Connection) Subscribe

func (c *Connection) Subscribe(subject string, cb nats.MsgHandler) (err error)

type Manager

type Manager interface {
	// Backends should return a list of current nats websocket endpoints.
	//   If the format is ws[s]://host:port then a websocket proxy will be used.
	//   If the format is http[s]://host:port then a httputil.ReverseProxy will be used.
	Backends() []string

	// TLSConfig passed to tls.Dial for testing tls backends.
	TLSConfig() *tls.Config

	// OnError will be called when errors occur within the websocket proxy only.
	OnError(message string, err error)

	// Randomize indicates Backends() should be shuffled before connection attempts.
	Randomize() bool

	// IsDebug will log all payloads on the websocket proxy only when true.
	IsDebug() bool
}

func StaticManager

func StaticManager(debug bool, backends ...string) Manager

type Proxy

type Proxy struct {
	Context context.Context
	Manager Manager
	// contains filtered or unexported fields
}

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(writer http.ResponseWriter, request *http.Request)

Directories

Path Synopsis
internal module

Jump to

Keyboard shortcuts

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