Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/spider-gazelle/crystal-mqtt
Crystal lang implementation of the MQTT protocol, a lightweight protocol for publish/subscribe messaging
https://github.com/spider-gazelle/crystal-mqtt
Last synced: 3 months ago
JSON representation
Crystal lang implementation of the MQTT protocol, a lightweight protocol for publish/subscribe messaging
- Host: GitHub
- URL: https://github.com/spider-gazelle/crystal-mqtt
- Owner: spider-gazelle
- License: mit
- Created: 2020-04-11T17:00:41.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-07-17T05:32:50.000Z (4 months ago)
- Last Synced: 2024-07-17T07:45:00.836Z (4 months ago)
- Language: Crystal
- Size: 36.1 KB
- Stars: 19
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - crystal-mqtt - A MQTT client (Network Protocols)
README
# Crystal MQTT
[![CI](https://github.com/spider-gazelle/crystal-mqtt/actions/workflows/ci.yml/badge.svg)](https://github.com/spider-gazelle/crystal-mqtt/actions/workflows/ci.yml)
A MQTT communication library for crystal lang with pluggable transports
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
mqtt:
github: spider-gazelle/crystal-mqtt
```2. Run `shards install`
## Usage
```crystal
require "mqtt/v3/client"# Create a transport (TCP, UDP, Websocket etc)
tls = OpenSSL::SSL::Context::Client.new
tls.verify_mode = OpenSSL::SSL::VerifyMode::NONE
transport = MQTT::Transport::TCP.new("test.mosquitto.org", 8883, tls)# Establish a MQTT connection
client = MQTT::V3::Client.new(transport)
client.connect# Perform some actions
client.publish("steves/channel", "hello", qos: MQTT::QoS::BrokerReceived)
client.ping# Subscribe to a channel
client.subscribe("$SYS/#") do |key, payload|
# payload is a Bytes slice (to support binary payloads)
content = String.new(payload)
puts "#{key}: #{content}"
endsleep 5
# Unsubscribe from a channel
client.unsubscribe("$SYS/#")sleep 1
# Pauses the fibre here until the socket closes (disconnect)
client.wait_close# You can also explicitly disconnect
client.disconnect```