# Connect to deployments with Go SDK

In this tutorial, you will learn to use Go paho-mqtt client to connect to EMQ X Cloud deployment.

# precondition

  • Already Create Deployment in EMQ X Cloud , and the deployment status is running

  • It is recommended to use Go 13+ version

  • Paho-mqtt MQTT client has been installed. If not installed, please run the following command to complete the installation

    go get github.com/eclipse/paho.mqtt.golang 
    go get github.com/gorilla/websocket
    go get golang.org/x/net/proxy
    
  • For free trial and shared deployment EMQ X Cloud, it provides TLS/SSL connection authentication by default, and you also need to download root certificate (opens new window) for TLS/SSL connection.

# Example Code

Get Sample Source Code (opens new window)

  • Connect to deployment and publish messages with MQTT protocol

    go run main.go --protocol=mqtt --port=1883 --action=pub
    
  • Connect to the deployment and subscribe to messages with MQTT protocol

    go run main.go --protocol=mqtt --port=1883 --action=sub
    
  • Connect to deployment, publish and subscribe to messages with MQTT protocol

    go run main.go --protocol=mqtt --port=1883 --action=pubsub
    
  • Connect to the deployment and publish messages with Websocket protocol

    go run main.go --protocol=ws --port=8083 --action=pub
    
  • Connect to the deployment and subscribe to messages with Websocket protocol

    go run main.go --protocol=ws --port=8083 --action=sub
    
  • Connect to deployment, publish and subscribe to messages with Websocket protocol

    go run main.go --protocol=ws --port=8083 --action=pubsub
    
  • Connect to deployment and publish messages with MQTT over TLS/SSL protocol

    go run main.go --protocol=mqtts --port=8883 --tls=true --action=pub
    
  • Connect to deployment and subscribe to messages with MQTT over TLS/SSL protocol

    go run main.go --protocol=mqtts --port=8883 --tls=true --action=sub
    
  • Connect to deployment, publish and subscribe to messages with MQTT over TLS/SSL protocol

    go run main.go --protocol=mqtts --port=8883 --tls=true --action=pubsub
    
  • Connect to deployment and publish messages with Websocket over TLS/SSL protocol

    go run main.go --protocol=wss --port=8084 --tls=true --action=pub
    
  • Connect to deployment and subscribe to messages with Websocket over TLS/SSL protocol

    go run main.go --protocol=wss --port=8084 --tls=true --action=sub
    
  • Connect to deployment,publish and subscribe to messages with Websocket over TLS/SSL protocol

    go run main.go --protocol=wss --port=8084 --tls=true --action=pubsub
    
  • How to use MQTT in Golang (opens new window)