ws

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

README

WebSocket adapter

WebSocket adapter provides a WebSocket API for sending and receiving messages through the platform.

Configuration

The service is configured using the environment variables presented in the following table. Note that any unset variables will be replaced with their default values.

Variable Description Default
MG_WS_ADAPTER_LOG_LEVEL Log level for the WS Adapter (debug, info, warn, error) info
MG_WS_ADAPTER_HTTP_HOST Service WS host ""
MG_WS_ADAPTER_HTTP_PORT Service WS port 8190
MG_WS_ADAPTER_HTTP_SERVER_CERT Path to the PEM encoded server certificate file ""
MG_WS_ADAPTER_HTTP_SERVER_KEY Path to the PEM encoded server key file ""
MG_THINGS_AUTH_GRPC_URL Things service Auth gRPC URL localhost:7000
MG_THINGS_AUTH_GRPC_TIMEOUT Things service Auth gRPC request timeout in seconds 1s
MG_THINGS_AUTH_GRPC_CLIENT_CERT Path to the PEM encoded things service Auth gRPC client certificate file ""
MG_THINGS_AUTH_GRPC_CLIENT_KEY Path to the PEM encoded things service Auth gRPC client key file ""
MG_THINGS_AUTH_GRPC_SERVER_CERTS Path to the PEM encoded things server Auth gRPC server trusted CA certificate file ""
MG_MESSAGE_BROKER_URL Message broker instance URL nats://localhost:4222
MG_JAEGER_URL Jaeger server URL http://localhost:14268/api/traces
MG_JAEGER_TRACE_RATIO Jaeger sampling ratio 1.0
MG_SEND_TELEMETRY Send telemetry to magistrala call home server true
MG_WS_ADAPTER_INSTANCE_ID Service instance ID ""

Deployment

The service is distributed as Docker container. Check the ws-adapter service section in docker-compose to see how the service is deployed.

Running this service outside of container requires working instance of the message broker service, things service and Jaeger server. To start the service outside of the container, execute the following shell script:

# download the latest version of the service
git clone https://github.com/absmach/magistrala

cd magistrala

# compile the ws
make ws

# copy binary to bin
make install

# set the environment variables and run the service
MG_WS_ADAPTER_LOG_LEVEL=info \
MG_WS_ADAPTER_HTTP_HOST=localhost \
MG_WS_ADAPTER_HTTP_PORT=8190 \
MG_WS_ADAPTER_HTTP_SERVER_CERT="" \
MG_WS_ADAPTER_HTTP_SERVER_KEY="" \
MG_THINGS_AUTH_GRPC_URL=localhost:7000 \
MG_THINGS_AUTH_GRPC_TIMEOUT=1s \
MG_THINGS_AUTH_GRPC_CLIENT_CERT="" \
MG_THINGS_AUTH_GRPC_CLIENT_KEY="" \
MG_THINGS_AUTH_GRPC_SERVER_CERTS="" \
MG_MESSAGE_BROKER_URL=nats://localhost:4222 \
MG_JAEGER_URL=http://localhost:14268/api/traces \
MG_JAEGER_TRACE_RATIO=1.0 \
MG_SEND_TELEMETRY=true \
MG_WS_ADAPTER_INSTANCE_ID="" \
$GOBIN/magistrala-ws

Setting MG_WS_ADAPTER_HTTP_SERVER_CERT and MG_WS_ADAPTER_HTTP_SERVER_KEY will enable TLS against the service. The service expects a file in PEM format for both the certificate and the key.

Setting MG_THINGS_AUTH_GRPC_CLIENT_CERT and MG_THINGS_AUTH_GRPC_CLIENT_KEY will enable TLS against the things service. The service expects a file in PEM format for both the certificate and the key. Setting MG_THINGS_AUTH_GRPC_SERVER_CERTS will enable TLS against the things service trusting only those CAs that are provided. The service expects a file in PEM format of trusted CAs.

Usage

For more information about service capabilities and its usage, please check out the WebSocket section.

Documentation

Overview

Package ws provides domain concept definitions required to support Magistrala WebSocket adapter service functionality.

This package defines the core domain concepts and types necessary to handle WebSocket connections and messages in the context of a Magistrala WebSocket adapter service. It abstracts the underlying complexities of WebSocket communication and provides a structured approach to working with WebSocket clients and servers.

For more details about Magistrala messaging and WebSocket adapter service, please refer to the documentation at https://docs.mainflux.io/messaging/#websocket.

Index

Constants

View Source
const (
	LogInfoSubscribed   = "subscribed with client_id %s to topics %s"
	LogInfoUnsubscribed = "unsubscribed client_id %s from topics %s"
	LogInfoConnected    = "connected with client_id %s"
	LogInfoDisconnected = "disconnected client_id %s and username %s"
	LogInfoPublished    = "published with client_id %s to the topic %s"
)

Log message formats.

Variables

View Source
var (
	// ErrFailedMessagePublish indicates that message publishing failed.
	ErrFailedMessagePublish = errors.New("failed to publish message")

	// ErrFailedSubscription indicates that client couldn't subscribe to specified channel.
	ErrFailedSubscription = errors.New("failed to subscribe to a channel")

	// ErrFailedUnsubscribe indicates that client couldn't unsubscribe from specified channel.
	ErrFailedUnsubscribe = errors.New("failed to unsubscribe from a channel")

	// ErrFailedConnection indicates that service couldn't connect to message broker.
	ErrFailedConnection = errors.New("failed to connect to message broker")

	// ErrInvalidConnection indicates that client couldn't subscribe to message broker.
	ErrInvalidConnection = errors.New("nats: invalid connection")

	// ErrUnauthorizedAccess indicates that client provided missing or invalid credentials.
	ErrUnauthorizedAccess = errors.New("missing or invalid credentials provided")

	// ErrEmptyTopic indicate absence of thingKey in the request.
	ErrEmptyTopic = errors.New("empty topic")

	// ErrEmptyID indicate absence of channelID in the request.
	ErrEmptyID = errors.New("empty id")
)
View Source
var (
	ErrMalformedSubtopic            = errors.New("malformed subtopic")
	ErrClientNotInitialized         = errors.New("client is not initialized")
	ErrMalformedTopic               = errors.New("malformed topic")
	ErrMissingClientID              = errors.New("client_id not found")
	ErrMissingTopicPub              = errors.New("failed to publish due to missing topic")
	ErrMissingTopicSub              = errors.New("failed to subscribe due to missing topic")
	ErrFailedConnect                = errors.New("failed to connect")
	ErrFailedSubscribe              = errors.New("failed to subscribe")
	ErrFailedPublish                = errors.New("failed to publish")
	ErrFailedDisconnect             = errors.New("failed to disconnect")
	ErrFailedPublishDisconnectEvent = errors.New("failed to publish disconnect event")
	ErrFailedParseSubtopic          = errors.New("failed to parse subtopic")
	ErrFailedPublishConnectEvent    = errors.New("failed to publish connect event")
	ErrFailedPublishToMsgBroker     = errors.New("failed to publish to magistrala message broker")
)

Error wrappers for MQTT errors.

Functions

func NewHandler

func NewHandler(pubsub messaging.PubSub, logger *slog.Logger, authClient magistrala.AuthzServiceClient) session.Handler

NewHandler creates new Handler entity.

Types

type Client

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

Client handles messaging and websocket connection.

func NewClient

func NewClient(c *websocket.Conn) *Client

NewClient returns a new websocket client.

func (*Client) Cancel

func (c *Client) Cancel() error

Cancel handles the websocket connection after unsubscribing.

func (*Client) Handle

func (c *Client) Handle(msg *messaging.Message) error

Handle handles the sending and receiving of messages via the broker.

type Service

type Service interface {
	// Subscribe subscribes message from the broker using the thingKey for authorization,
	// and the channelID for subscription. Subtopic is optional.
	// If the subscription is successful, nil is returned otherwise error is returned.
	Subscribe(ctx context.Context, thingKey, chanID, subtopic string, client *Client) error
}

Service specifies web socket service API.

func New

func New(authClient magistrala.AuthzServiceClient, pubsub messaging.PubSub) Service

New instantiates the WS adapter implementation.

Directories

Path Synopsis
Package api contains API-related concerns: endpoint definitions, middlewares and all resource representations.
Package api contains API-related concerns: endpoint definitions, middlewares and all resource representations.
Package tracing provides tracing instrumentation for Magistrala WebSocket adapter service.
Package tracing provides tracing instrumentation for Magistrala WebSocket adapter service.

Jump to

Keyboard shortcuts

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