Categorygithub.com/golain-io/mqtt-bridge
modulepackage
0.1.3
Repository: https://github.com/golain-io/mqtt-bridge.git
Documentation: pkg.go.dev

# README

MQTT-Bridge

MQTT-Bridge is a library that allows you to bridge protocols over MQTT, with a focus on gRPC. It provides two implementations:

  • Network Bridge: A low-level implementation that allows existing gRPC clients and servers to communicate over MQTT without modification. The network bridge provides a net.Listener and net.Conn interface.
  • gRPC Bridge: A higher-level implementation that works directly with MQTT messages while maintaining gRPC-style APIs. The gRPC bridge provides a grpc.ServiceRegistrar and grpc.ServiceInfoProvider interface.

In theory, the network bridge should work with anything that uses the net.Listener and net.Conn interfaces, such as HTTP servers and other networking libraries.

Example Overview

This example demonstrates how to use mqtt-bridge to enable gRPC-style communication over MQTT. The project includes two different implementations showing how to bridge gRPC and MQTT communications.

Overview

The example implements a simple Echo service with three types of RPCs:

  • Unary calls (simple request-response)
  • Server streaming (server sends multiple responses)
  • Bidirectional streaming (both client and server can send multiple messages)

Prerequisites

  • Go 1.19 or later
  • An MQTT broker (e.g., Mosquitto) running on localhost:1883
  • Protocol buffer compiler (protoc)

Service Definition

The Echo service is defined in the proto file:

startLine: 6
endLine: 15

Implementation Options

1. Network Bridge Implementation

The network bridge provides a low-level network implementation that allows existing gRPC clients and servers to communicate over MQTT without modification.

Server Setup

startLine: 34
endLine: 45

Client Setup

startLine: 37
endLine: 55

To run:

# Start the server
go run example/net_bridge/server/main.go

# In another terminal, start the client
go run example/net_bridge/client/main.go

2. gRPC Bridge Implementation

The gRPC bridge provides a higher-level abstraction that works directly with MQTT messages while maintaining gRPC-style APIs.

Server Setup

startLine: 33
endLine: 39

Client Setup

startLine: 30
endLine: 51

To run:

# Start the server
go run example/grpc_bridge/server/main.go

# In another terminal, start the client
go run example/grpc_bridge/client/main.go

Service Implementation

The Echo service implements three types of RPCs:

  1. Unary Call - Simple request-response:
startLine: 23
endLine: 33
  1. Server Streaming - Server sends multiple responses:
startLine: 36
endLine: 55
  1. Bidirectional Streaming - Both sides can send messages:
startLine: 58
endLine: 82

Key Features

  • Seamless conversion between gRPC and MQTT communication
  • Support for all gRPC communication patterns:
    • Unary calls
    • Server streaming
    • Client streaming
    • Bidirectional streaming
  • Automatic message framing and protocol handling
  • Integration with existing gRPC tooling
  • Choice between network-level and message-level implementations

Notes

  • The network bridge implementation is ideal when you want to use existing gRPC code over MQTT
  • The gRPC bridge implementation is better when you want to work directly with MQTT messages while maintaining gRPC-style APIs
  • Both implementations support the full range of gRPC features
  • Ensure your MQTT broker is properly configured and accessible before running the examples
  • The gRPC bridge implementation has not been tested for streaming RPCs, only unary has been tested as of now.

License

MIT License

Copyright

Copyright 2024 Golain Systems Private Limited.

# Packages

No description provided by the author
No description provided by the author

# Functions

BuildTopicPath creates an MQTT topic path for a given service and method.
#nosec G103 CopyBytes copies a slice to make it immutable.
CopyString copies a string to make it immutable.
FrameMessage splits a large message into frames.
NewFrame creates a new Frame with validated parameters.
Add a new helper for creating large messages.
No description provided by the author
NewMQTTNetBridge creates a new bridge that listens on a specific bridgeID.
NewSession creates a new session with proper initialization.
ParseTopicPath extracts components from an MQTT topic path.
Add a helper to return large message buffers to the pool.
No description provided by the author
UnmarshalFrame converts a byte slice into a Frame.
UnsafeBytes returns a byte pointer without allocation.
UnsafeString returns a string pointer without allocation.
WithFragmentation sets fragmentation parameters for a frame.
WithLogger sets the logger for the bridge.
WithMQTTClient sets the MQTT client for the bridge.
WithQoS sets the MQTT QoS level for the bridge.
WithRootTopic sets the root topic for the bridge.
WithSessionID sets a specific session ID for connection.
WithSessionState sets the initial session state.
WithStreamID sets the StreamID for a frame.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
Maximum sequence number (uint64 max).
Maximum length for StreamID.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Fixed header size without StreamID.
Minimum sequence number.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

Fixed header size.
10KB per fragment.

# Structs

BridgeConfig holds bridge-specific configuration.
No description provided by the author
BridgeHooks manages a collection of bridge hooks.
Frame represents a complete protocol frame including header and payload.
No description provided by the author
MQTTAddr implements net.Addr for MQTT connections.
No description provided by the author
MQTTNetBridge implements net.Listener over MQTT.
MQTTNetBridgeConn implements net.Conn over MQTT.
Session represents an active MQTT-GRPC bridge session.
SessionConfig holds session-specific configuration.
SessionInfo tracks session state and metadata.
StreamContext holds the context for an active stream.

# Interfaces

BridgeHook provides an interface for handling bridge-related events.

# Type aliases

BridgeOption configures bridge behavior.
BridgeSessionState represents the current state of a bridge session.
FrameOption defines options for frame creation.
No description provided by the author
SessionEvent represents different session lifecycle events.
SessionEventHandler is called when session state changes occur.
SessionOption configures session behavior.
No description provided by the author
No description provided by the author