- StreamNative Platform
- Connect
Connect to Pulsar clusters using Eclipse Mosquitto
StreamNative Platform brings native MQTT protocol support to Pulsar brokers using the MQTT on Pulsar (MoP) protocol handler. Therefore, you can migrate your existing MQTT applications and services to Apache Pulsar without modifying the code.
You can use any client that supports the MQTT protocol to publish data into Pulsar. In this document, we use the Eclipse Mosquitto MQTT CLI tool to connect to a Pulsar cluster and then produce and consume messages to and from the Pulsar cluster.
Prerequisites
- Install StreamNative Platform and run a Pulsar cluster. For details, see deploy StreamNative Platform.
- Enable MoP. For details, see enable MoP.
- Get the Web service URL of your Pulsar cluster or broker service and the token used to access your Pulsar cluster. For details, see prepare to connect to Pulsar clusters.
Procedures
To connect to a Pulsar cluster using the Eclipse Mosquitto MQTT CLI tool, follow these steps.
Deploy an Eclipse Mosquitto Pod.
a. Define an Eclipse Mosquitto as below and save the YAML file (
pod.yaml
).apiVersion: v1 kind: Pod metadata: name: mosquitto labels: app: mosquitto spec: containers: - name: mqtt-cli image: eclipse-mosquitto imagePullPolicy: IfNotPresent command: - sleep - 3600s
b. Apply your configurations.
kubectl apply -f pod.yaml
Subscribe to messages from the Pulsar cluster.
a. Open a new terminal window and enter the Eclipse Mosquitto Pod.
kubectl exec -it mosquitto -- sh
b. Subscribe to messages from the Pulsar cluster.
mosquitto_sub -h <broker_service> -p 5682 -t <topic_name>
If you have enabled authentication, use the token as the password. The username can be any string.
mosquitto_sub -h <broker_service> -p 5682 -u mqtt -P <token> -t <topic_name>
Publish messages to the Pulsar cluster.
a. Open a new terminal window and enter the Eclipse Mosquitto Pod.
kubectl exec -it mosquitto -- sh
b. Publish messages to the Pulsar cluster.
mosquitto_pub -h <broker_service> -p 5682 -t <topic_name> -m "hello pulsar"
If you have enabled authentication, execute the following command.
mosquitto_pub -h <broker_service> -p 5682 -u mqtt -p <token> -t <topic_name> -m "hello pulsar"