{"id":20432178,"url":"https://github.com/davidfantasy/embedded-mqtt-broker","last_synced_at":"2025-09-24T11:31:28.295Z","repository":{"id":164832598,"uuid":"640252415","full_name":"davidfantasy/embedded-mqtt-broker","owner":"davidfantasy","description":"go语言版本的mqtt服务器实现，非常轻量级，可嵌入到其它业务代码中","archived":false,"fork":false,"pushed_at":"2023-06-06T06:21:29.000Z","size":43,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-06T14:21:53.507Z","etag":null,"topics":["embedded","lightweight","mqtt","mqtt-server"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidfantasy.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":"security/authentication.go","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-13T13:32:32.000Z","updated_at":"2024-12-09T08:51:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"7ce6d54f-267f-4b77-802f-d24b415c0015","html_url":"https://github.com/davidfantasy/embedded-mqtt-broker","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/davidfantasy/embedded-mqtt-broker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidfantasy%2Fembedded-mqtt-broker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidfantasy%2Fembedded-mqtt-broker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidfantasy%2Fembedded-mqtt-broker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidfantasy%2Fembedded-mqtt-broker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidfantasy","download_url":"https://codeload.github.com/davidfantasy/embedded-mqtt-broker/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidfantasy%2Fembedded-mqtt-broker/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276738878,"owners_count":25696014,"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","status":"online","status_checked_at":"2025-09-24T02:00:09.776Z","response_time":97,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["embedded","lightweight","mqtt","mqtt-server"],"created_at":"2024-11-15T08:14:05.568Z","updated_at":"2025-09-24T11:31:28.290Z","avatar_url":"https://github.com/davidfantasy.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# embedded-mqtt-broker\n go语言版本的mqtt轻量级服务器实现，开发者可以自由的在此基础上进行定制，实现更为复杂的业务功能。主要特性有：\n- 不依赖任何其它中间件，非常轻量； \n- 可以灵活嵌入到其它任何go语言程序中；\n- 完整支持MQTT 3.1.1协议；\n\n# 使用方式\n\n## 下载依赖\n```\ngo get github.com/davidfantasy/embedded-mqtt-broker\n```\n\n## 代码示例\n```go\nimport mqtt \"github.com/davidfantasy/embedded-mqtt-broker\"\n\nfunc main() {\n\tconfig := config.NewDefaultConfig()\n\tbroker := mqtt.NewMqttServer(config)\n\tbroker.Startup()\n}\n\n```\n## 配置项\n```go\nfunc NewDefaultConfig() *ServerConfig {\n\treturn \u0026ServerConfig{\n\t\t//服务监听地址\n\t\tAddress: \"0.0.0.0\",\n\t\t//服务监听端口\n\t\tPort: 1883,\n\t\t//默认的会话超时时间，客户端断联超过该时间后，其订阅信息及其它与会话绑定的消息都将被清除\n\t\tSessionExpiryInterval: time.Hour * 2,\n\t}\n}\n```\n## 权限控制\n现在mqtt broker可以指定接入客户端的访问控制权限，开发者可以自定义一个**security.AuthenticationProvider**，并根据接入客户端的验证信息返回不同的权限，包括对topic的publis和subcribe的权限。示例代码如下：\n```go\nimport (\n\tmqtt \"github.com/davidfantasy/embedded-mqtt-broker\"\n\t\"github.com/davidfantasy/embedded-mqtt-broker/security\"\n)\n\ntype CustomAuthManager struct {\n}\n\nfunc main() {\n\tconfig := mqtt.NewServerOptions()\n\tbroker := mqtt.NewMqttServer(config)\n\t//添加权限管理器\n\tbroker.SetAuthProvider(\u0026CustomAuthManager{})\n\tbroker.Startup()\n}\n\n//自定义权限管理器，实现AuthenticationProvider接口\nfunc (manager *CustomAuthManager) Authenticate(username, password string) *security.Authentication {\n\tif username == \"admin\" \u0026\u0026 password == \"psw\" {\n\t\treturn security.NewAuthentication([]security.Acl{{Topic: \"admin/#\", Access: security.CanSubPub}})\n\t} else if username == \"user\" \u0026\u0026 password == \"psw\" {\n\t\treturn security.NewAuthentication([]security.Acl{{Topic: \"user/#\", Access: security.CanSubPub}})\n\t}\n\treturn nil\n}\n```\n## 日志\n默认DEBUG,INFO,WARN,ERROR日志都是使用os.Stdout/os.Stderr进行输出，如果需要将日志记录到其它输出流，可为不同级别的logger提供一个自定义的实现，例如：\n~~~go\ntype NOOPLogger struct{}\n\n//实现日志接口\nfunc (NOOPLogger) Println(v ...interface{})               {}\nfunc (NOOPLogger) Printf(format string, v ...interface{}) {}\n\nfunc changeLogger() {\n\t//将debug的logger替换为空输出，这样就不会输出debug日志\n\tlogger.DEBUG = NOOPLogger{}\n}\n~~~\n# todos\n1. 支持保留消息（RETAIN）\n2. 支持QOS大于0的消息的收发\n3. 性能测试和优化\n\n后续会不断完善相关功能\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidfantasy%2Fembedded-mqtt-broker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidfantasy%2Fembedded-mqtt-broker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidfantasy%2Fembedded-mqtt-broker/lists"}