{"id":22181001,"url":"https://github.com/easygithdev/mqtt","last_synced_at":"2025-06-22T00:36:49.494Z","repository":{"id":57659858,"uuid":"473508687","full_name":"EasyGithDev/mqtt","owner":"EasyGithDev","description":"Implement MQTT client","archived":false,"fork":false,"pushed_at":"2022-04-18T13:46:59.000Z","size":181,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T19:16:22.815Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EasyGithDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-24T07:58:40.000Z","updated_at":"2022-03-24T08:05:44.000Z","dependencies_parsed_at":"2022-09-08T00:12:17.178Z","dependency_job_id":null,"html_url":"https://github.com/EasyGithDev/mqtt","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/EasyGithDev/mqtt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EasyGithDev%2Fmqtt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EasyGithDev%2Fmqtt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EasyGithDev%2Fmqtt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EasyGithDev%2Fmqtt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EasyGithDev","download_url":"https://codeload.github.com/EasyGithDev/mqtt/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EasyGithDev%2Fmqtt/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261217292,"owners_count":23126246,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-02T09:21:26.247Z","updated_at":"2025-06-22T00:36:44.476Z","avatar_url":"https://github.com/EasyGithDev.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mqtt client\nI'll try to implement the draft as mentionned here :\n\nhttps://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/\n\n\n## Use as a binary\n\nOpen a terminal and write :\n```bash\n    ./make\n```\n\nOr you can edit the make file to perfome your own buid command.\n\nYou will find the binary in the bin folder.\n\nYou can send a publish like above :\n```bash\n    ./bin/client -pub -h=test.mosquitto.org -t=hello/mqtt \"-m=hello world\"\n```\n\nYou can subscribe to a topic and read the messages like above :\n```bash\n    ./bin/client -sub -h=test.mosquitto.org -t=hello/mqtt\n```\n\n## Use as a library\n\n#### Connection\n\nSimple connect with :\n- clientId\n- hostname\n- port\n\n```go\n        clientId := \"test-golang-mqtt\"\n        connHost := \"test.mosquitto.org\"\n        connPort := \"1883\"\n\n        mc := client.New(\n            // client Id\n            clientId,\n            // connection infos\n            client.WithConnInfos(conn.New(connHost, conn.WithPort(connPort))),\n        )\n\n        _, connErr := mc.Connect()\n        if connErr != nil {\n            log.Print(\"Error connecting:\", connErr.Error())\n        }\n        defer mc.Close()\n```\n\nConnect with credentials :\n- Username\n- Password\n\n```go\n\n        ...\n\n        username := \"rw\"\n        password := \"readwrite\"\n        mc := client.New(\n            // client Id\n            clientId,\n            // Credentials\n            client.WithCredentials(username, password),\n            // connection infos\n            client.WithConnInfos(conn.New(connHost, conn.WithPort(connPort))),\n        )\n\n        ...\n\t\n```\n\n#### Publish\n\nPublish a message :\n- topic : the topic that the message should be published on.\n- message : the actual message to send.\n- qos : the quality of service level to use.\n- retain : if set to True, the message will be set as the “last known good”/retained message for the topic.\n\n```go\n        topic := \"hello/mqtt\"\n        qos := client.QOS_0\n        msg := \"The temperature is 5 degrees\"\n\n        _, pubErr := mc.Publish(topic, msg, byte(qos))\n\n        if pubErr != nil {\n            log.Print(\"Error publishing:\", pubErr.Error())\n        }\n```\n\nPublish many messages :\n\n```go\n\n        go mc.LoopStart()\n\n        for {\n            temperature := rand.Intn(60)\n            msg := \"The temperature is \" + fmt.Sprintf(\"%d\", temperature)\n            _, pubErr := mc.Publish(topic, msg, byte(qos))\n\n            if pubErr != nil {\n                log.Print(\"Error publishing:\", pubErr.Error())\n                break\n            }\n            time.Sleep(5 * time.Second)\n        }\n\n```\n\n#### Subscribe\n\n\nSubscribe with :\n- topic : a string specifying the subscription topic to subscribe to.\n- qos : the desired quality of service level for the subscription.\n\n```go\n        topic := \"hello/mqtt\"\n        qos := client.QOS_0\n\n        _, errSub := mc.Subscribe(topic, client.QOS_0)\n        if errSub != nil {\n            log.Printf(\"Subscribe Error: %s\\n\", errSub)\n        }\n```\n\nGet the messages :\n\n```go\n        _, errSub := mc.Subscribe(topic, client.QOS_0)\n        if errSub != nil {\n            log.Printf(\"Subscribe Error: %s\\n\", errSub)\n        } else {\n            mc.LoopForever()\n        }\n```\n\n#### Callback function\n\n```go\n        var onConnect = func(mc client.MqttClient, userData interface{}, rc net.Conn) {\n        fmt.Println(\"Connecting to server \" + rc.RemoteAddr().String())\n        }\n\n        var onDisconnect = func(mc client.MqttClient, userData interface{}, rc net.Conn) {\n        fmt.Println(\"Disconnect from server\" + rc.RemoteAddr().String())\n        }\n\n        var onPublish = func(mc client.MqttClient, userData interface{}, mid int) {\n        fmt.Printf(\"Publish\\n\")\n        }\n\n        var onSubscribe = func(mc client.MqttClient, userData interface{}, mid int) {\n        fmt.Printf(\"Subscribe\\n\")\n        }\n\n        var onMessage = func(mc client.MqttClient, userData interface{}, message string) {\n        fmt.Println(\"msg: \" + message)\n        }\n\n        ...\n\n        mc.OnConnect = onConnect\n        mc.OnDisconnect = onDisconnect\n        mc.OnPublish = onPublish\n        mc.OnSubscribe = onSubscribe\n        mc.OnMessage = onMessage\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feasygithdev%2Fmqtt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feasygithdev%2Fmqtt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feasygithdev%2Fmqtt/lists"}