modulepackage
0.0.0-20231026085029-09423e4a4df1
Repository: https://github.com/orus-io/nats-websocket-gw.git
Documentation: pkg.go.dev
# README
NATS <-> websocket gateway
A websocket gateway for NATS, usable as a backend for elm-nats and websocket-nats.
Features:
- TLS support
- Each NATS command is sent as a separate websocket message
- Provides a hook to change the CONNECT phase, allowing the http server to handle the connection itself (for example based on a cookie of the http request)
- Easily embeddable in a bigger http server
- Supports both text (default) and binary (by adding '?mode=binary' to the url) messages
Basic usage
Fetch the source:
go get -u github.com/orus-io/nats-websocket-gw
Install and run the default binary
go install github.com/orus-io/nats-websocket-gw/cmd/nats-websocket-gw
nats-websocket-gw --no-origin-check
and/or integrate it in your http server:
package main
import (
"net/http"
"github.com/orus-io/nats-websocket-gw"
)
func main() {
gateway := gw.NewGateway(gw.Settings{
NatsAddr: "localhost:4222",
})
http.HandleFunc("/nats", gateway.Handler)
http.ListenAndServe("0.0.0.0:8910", nil)
}
How does it differ from other nats-websocket servers ?
-
Rest to NATS Proxy provides a websocket based implementation. The approach is pretty different though, as the websockets do not transport the whole NATS protocol, but only the data of the messages (websockets are opened for each subscribed subject).
-
ws-tcp-relay, which the proposed backend for websocket-nats is a generic ws-tcp gateway. As such, it:
- does not allow for TLS connections to NATS because the TLS negociation cannot be done immediately (the NATS protocol has a clear text 'INFO' exchange before TLS handshake)
- send websocket messages that may contain several NATS commands.
# Packages
No description provided by the author
# Functions
NewCommandsReader creates a CommandsReader.
NewGateway instanciates a Gateway.
# Structs
CommandsReader parses a NATS connection input stream into commands.
Gateway is a HTTP handler that acts as a websocket gateway to a NATS server.
NatsConn holds a NATS TCP connection.
Settings configures a Gateway.
# Type aliases
ConnectHandler is used in Settings for handling the initial CONNECT of a nats connection.
ErrorHandler is used in Settings for handling errors.
NatsServerInfo is the information returned by the INFO nats message.