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.Listenerandnet.Conninterface. - gRPC Bridge: A higher-level implementation that works directly with MQTT messages while maintaining gRPC-style APIs. The gRPC bridge provides a
grpc.ServiceRegistrarandgrpc.ServiceInfoProviderinterface.
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.
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.
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)
- Go 1.19 or later
- An MQTT broker (e.g., Mosquitto) running on localhost:1883
- Protocol buffer compiler (protoc)
The Echo service is defined in the proto file:
startLine: 6
endLine: 15The network bridge provides a low-level network implementation that allows existing gRPC clients and servers to communicate over MQTT without modification.
startLine: 34
endLine: 45startLine: 37
endLine: 55To 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.goThe gRPC bridge provides a higher-level abstraction that works directly with MQTT messages while maintaining gRPC-style APIs.
startLine: 33
endLine: 39startLine: 30
endLine: 51To 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.goThe Echo service implements three types of RPCs:
- Unary Call - Simple request-response:
startLine: 23
endLine: 33- Server Streaming - Server sends multiple responses:
startLine: 36
endLine: 55- Bidirectional Streaming - Both sides can send messages:
startLine: 58
endLine: 82- 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
- 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.
MIT License
Copyright 2024 Golain Systems Private Limited.