{"id":20478920,"url":"https://github.com/r3labs/sse","last_synced_at":"2025-05-14T16:09:54.174Z","repository":{"id":37549636,"uuid":"52105805","full_name":"r3labs/sse","owner":"r3labs","description":"Server Sent Events server and client for Golang","archived":false,"fork":false,"pushed_at":"2024-06-26T04:19:05.000Z","size":154,"stargazers_count":919,"open_issues_count":44,"forks_count":183,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-04-13T01:54:59.081Z","etag":null,"topics":["eventstream","golang","sse"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/r3labs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-19T17:52:22.000Z","updated_at":"2025-04-11T17:09:36.000Z","dependencies_parsed_at":"2023-02-13T08:46:04.548Z","dependency_job_id":"bfefe3ca-87be-4048-8a4f-dc11a009dc4b","html_url":"https://github.com/r3labs/sse","commit_stats":null,"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/r3labs%2Fsse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/r3labs%2Fsse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/r3labs%2Fsse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/r3labs%2Fsse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/r3labs","download_url":"https://codeload.github.com/r3labs/sse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248654051,"owners_count":21140235,"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":["eventstream","golang","sse"],"created_at":"2024-11-15T15:39:50.050Z","updated_at":"2025-04-13T01:55:19.240Z","avatar_url":"https://github.com/r3labs.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# SSE - Server Sent Events Client/Server Library for Go\n\n## Synopsis\n\nSSE is a client/server implementation for Server Sent Events for Golang.\n\n## Build status\n\n* Master: [![CircleCI  Master](https://circleci.com/gh/r3labs/sse.svg?style=svg)](https://circleci.com/gh/r3labs/sse)\n\n## Quick start\n\nTo install:\n```\ngo get github.com/r3labs/sse/v2\n```\n\nTo Test:\n\n```sh\n$ make deps\n$ make test\n```\n\n#### Example Server\n\nThere are two parts of the server. It is comprised of the message scheduler and a http handler function.\nThe messaging system is started when running:\n\n```go\nfunc main() {\n\tserver := sse.New()\n}\n```\n\nTo add a stream to this handler:\n\n```go\nfunc main() {\n\tserver := sse.New()\n\tserver.CreateStream(\"messages\")\n}\n```\n\nThis creates a new stream inside of the scheduler. Seeing as there are no consumers, publishing a message to this channel will do nothing.\nClients can connect to this stream once the http handler is started by specifying _stream_ as a url parameter, like so:\n\n```\nhttp://server/events?stream=messages\n```\n\n\nIn order to start the http server:\n\n```go\nfunc main() {\n\tserver := sse.New()\n\n\t// Create a new Mux and set the handler\n\tmux := http.NewServeMux()\n\tmux.HandleFunc(\"/events\", server.ServeHTTP)\n\n\thttp.ListenAndServe(\":8080\", mux)\n}\n```\n\nTo publish messages to a stream:\n\n```go\nfunc main() {\n\tserver := sse.New()\n\n\t// Publish a payload to the stream\n\tserver.Publish(\"messages\", \u0026sse.Event{\n\t\tData: []byte(\"ping\"),\n\t})\n}\n```\n\nPlease note there must be a stream with the name you specify and there must be subscribers to that stream\n\nA way to detect disconnected clients:\n\n```go\nfunc main() {\n\tserver := sse.New()\n\n\tmux := http.NewServeMux()\n\tmux.HandleFunc(\"/events\", func(w http.ResponseWriter, r *http.Request) {\n\t\tgo func() {\n\t\t\t// Received Browser Disconnection\n\t\t\t\u003c-r.Context().Done()\n\t\t\tprintln(\"The client is disconnected here\")\n\t\t\treturn\n\t\t}()\n\n\t\tserver.ServeHTTP(w, r)\n\t})\n\n\thttp.ListenAndServe(\":8080\", mux)\n}\n```\n\n#### Example Client\n\nThe client exposes a way to connect to an SSE server. The client can also handle multiple events under the same url.\n\nTo create a new client:\n\n```go\nfunc main() {\n\tclient := sse.NewClient(\"http://server/events\")\n}\n```\n\nTo subscribe to an event stream, please use the Subscribe function. This accepts the name of the stream and a handler function:\n\n```go\nfunc main() {\n\tclient := sse.NewClient(\"http://server/events\")\n\n\tclient.Subscribe(\"messages\", func(msg *sse.Event) {\n\t\t// Got some data!\n\t\tfmt.Println(msg.Data)\n\t})\n}\n```\n\nPlease note that this function will block the current thread. You can run this function in a go routine.\n\nIf you wish to have events sent to a channel, you can use SubscribeChan:\n\n```go\nfunc main() {\n\tevents := make(chan *sse.Event)\n\n\tclient := sse.NewClient(\"http://server/events\")\n\tclient.SubscribeChan(\"messages\", events)\n}\n```\n\n#### HTTP client parameters\n\nTo add additional parameters to the http client, such as disabling ssl verification for self signed certs, you can override the http client or update its options:\n\n```go\nfunc main() {\n\tclient := sse.NewClient(\"http://server/events\")\n\tclient.Connection.Transport =  \u0026http.Transport{\n\t\tTLSClientConfig: \u0026tls.Config{InsecureSkipVerify: true},\n\t}\n}\n```\n\n#### URL query parameters\n\nTo set custom query parameters on the client or disable the stream parameter altogether:\n\n```go\nfunc main() {\n\tclient := sse.NewClient(\"http://server/events?search=example\")\n\n\tclient.SubscribeRaw(func(msg *sse.Event) {\n\t\t// Got some data!\n\t\tfmt.Println(msg.Data)\n\t})\n}\n```\n\n\n## Contributing\n\nPlease read through our\n[contributing guidelines](CONTRIBUTING.md).\nIncluded are directions for opening issues, coding standards, and notes on\ndevelopment.\n\nMoreover, if your pull request contains patches or features, you must include\nrelevant unit tests.\n\n## Versioning\n\nFor transparency into our release cycle and in striving to maintain backward\ncompatibility, this project is maintained under [the Semantic Versioning guidelines](http://semver.org/).\n\n## Copyright and License\n\nCode and documentation copyright since 2015 r3labs.io authors.\n\nCode released under\n[the Mozilla Public License Version 2.0](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fr3labs%2Fsse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fr3labs%2Fsse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fr3labs%2Fsse/lists"}