{"id":19339732,"url":"https://github.com/rubenv/broadcaster","last_synced_at":"2025-04-23T02:30:54.243Z","repository":{"id":27184817,"uuid":"30654736","full_name":"rubenv/broadcaster","owner":"rubenv","description":"Websocket server for broadcasting Redis pub/sub messages to web clients.","archived":false,"fork":false,"pushed_at":"2021-09-03T09:28:14.000Z","size":113,"stargazers_count":35,"open_issues_count":0,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T06:43:12.459Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rubenv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-11T15:37:50.000Z","updated_at":"2024-12-12T16:39:16.000Z","dependencies_parsed_at":"2022-08-17T17:10:45.238Z","dependency_job_id":null,"html_url":"https://github.com/rubenv/broadcaster","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenv%2Fbroadcaster","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenv%2Fbroadcaster/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenv%2Fbroadcaster/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenv%2Fbroadcaster/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rubenv","download_url":"https://codeload.github.com/rubenv/broadcaster/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250357567,"owners_count":21417308,"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-11-10T03:23:34.833Z","updated_at":"2025-04-23T02:30:53.985Z","avatar_url":"https://github.com/rubenv.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# broadcaster\n\n[![Build Status](https://travis-ci.org/rubenv/broadcaster.svg?branch=master)](https://travis-ci.org/rubenv/broadcaster) [![GoDoc](https://godoc.org/github.com/rubenv/broadcaster?status.png)](https://godoc.org/github.com/rubenv/broadcaster)\n\nPackage broadcaster implements a websocket server for broadcasting Redis pub/sub\nmessages to web clients.\n\nA JavaScript client can be found here:\nhttps://github.com/rubenv/broadcaster-client\n\nOriginally based on https://github.com/rubenv/node-broadcast-hub but\nsignificantly improved while moving to Go.\n\n## Installation\n```\ngo get github.com/rubenv/broadcaster\n```\n\nImport into your application with:\n\n```go\nimport \"github.com/rubenv/broadcaster\"\n```\n\n## Usage\n\n```go\nconst (\n\t// Client: start authentication\n\tAuthMessage = \"auth\"\n\n\t// Server: Authentication succeeded\n\tAuthOKMessage = \"authOk\"\n\n\t// Server: Authentication failed\n\tAuthFailedMessage = \"authError\"\n\n\t// Client: Subscribe to channel\n\tSubscribeMessage = \"subscribe\"\n\n\t// Server: Subscribe succeeded\n\tSubscribeOKMessage = \"subscribeOk\"\n\n\t// Server: Subscribe failed\n\tSubscribeErrorMessage = \"subscribeError\"\n\n\t// Server: Broadcast message\n\tMessageMessage = \"message\"\n\n\t// Client: Unsubscribe from channel\n\tUnsubscribeMessage = \"unsubscribe\"\n\n\t// Server: Unsubscribe succeeded\n\tUnsubscribeOKMessage = \"unsubscribeOk\"\n\n\t// Server: Unsubscribe failed\n\tUnsubscribeErrorMessage = \"unsubscribeError\"\n\n\t// Client: Send me more messages\n\tPollMessage = \"poll\"\n\n\t// Client: I'm still alive\n\tPingMessage = \"ping\"\n\n\t// Server: Unknown message\n\tUnknownMessage = \"unknown\"\n\n\t// Server: Server error\n\tServerErrorMessage = \"serverError\"\n)\n```\nMessage types used between server and client.\n\n#### type Client\n\n```go\ntype Client struct {\n\tMode ClientMode\n\n\t// Data passed when authenticating\n\tAuthData map[string]interface{}\n\n\t// Set when disconnecting\n\tError error\n\n\t// Incoming messages\n\tMessages chan ClientMessage\n\n\t// Receives true when disconnected\n\tDisconnected chan bool\n\n\t// Timeout\n\tTimeout time.Duration\n\n\t// Ping interval\n\tPingInterval time.Duration\n\n\t// Reconnection attempts\n\tMaxAttempts int\n\n\t// Can be overwritten\n\tUserAgent string\n}\n```\n\n\n#### func  NewClient\n\n```go\nfunc NewClient(urlStr string) (*Client, error)\n```\n\n#### func (*Client) Connect\n\n```go\nfunc (c *Client) Connect() error\n```\n\n#### func (*Client) Disconnect\n\n```go\nfunc (c *Client) Disconnect() error\n```\n\n#### func (*Client) Subscribe\n\n```go\nfunc (c *Client) Subscribe(channel string) error\n```\n\n#### func (*Client) Unsubscribe\n\n```go\nfunc (c *Client) Unsubscribe(channel string) error\n```\n\n#### type ClientMessage\n\n```go\ntype ClientMessage map[string]interface{}\n```\n\n\n#### func (ClientMessage) Channel\n\n```go\nfunc (c ClientMessage) Channel() string\n```\n\n#### func (ClientMessage) ResultId\n\n```go\nfunc (c ClientMessage) ResultId() string\n```\n\n#### func (ClientMessage) Token\n\n```go\nfunc (c ClientMessage) Token() string\n```\n\n#### func (ClientMessage) Type\n\n```go\nfunc (c ClientMessage) Type() string\n```\n\n#### type ClientMode\n\n```go\ntype ClientMode int\n```\n\nClient connection mode.\n\n```go\nconst (\n\tClientModeAuto      ClientMode = 0\n\tClientModeWebsocket ClientMode = 1\n\tClientModeLongPoll  ClientMode = 2\n)\n```\nConnection modes, can be used to force a specific connection type.\n\n#### type Server\n\n```go\ntype Server struct {\n\t// Invoked upon initial connection, can be used to enforce access control.\n\tCanConnect func(data map[string]interface{}) bool\n\n\t// Invoked upon channel subscription, can be used to enforce access control\n\t// for channels.\n\tCanSubscribe func(data map[string]interface{}, channel string) bool\n\n\t// Can be set to allow CORS requests.\n\tCheckOrigin func(r *http.Request) bool\n\n\t// Can be used to configure buffer sizes etc.\n\t// See http://godoc.org/github.com/gorilla/websocket#Upgrader\n\tUpgrader websocket.Upgrader\n\n\t// Redis host, used for data, defaults to localhost:6379\n\tRedisHost string\n\n\t// Redis pubsub channel, used for internal coordination messages\n\t// Defaults to \"broadcaster\"\n\tControlChannel string\n\n\t// Namespace for storing session data.\n\t// Defaults to \"bc:\"\n\tControlNamespace string\n\n\t// PubSub host, used for pubsub, defaults to RedisHost\n\tPubSubHost string\n\n\t// Timeout for long-polling connections\n\tTimeout time.Duration\n\n\t// Combine long poll message for given duration (more latency, less load)\n\tPollTime time.Duration\n}\n```\n\nA Server is the main class of this package, pass it to http.Handle on a chosen\npath to start a broadcast server.\n\n#### func (*Server) Prepare\n\n```go\nfunc (s *Server) Prepare() error\n```\n\n#### func (*Server) ServeHTTP\n\n```go\nfunc (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)\n```\nMain HTTP server.\n\n#### func (*Server) Stats\n\n```go\nfunc (s *Server) Stats() (Stats, error)\n```\n\n#### type Stats\n\n```go\ntype Stats struct {\n\t// Number of active connections\n\tConnections int\n\n\t// For debugging purposes only\n\tLocalSubscriptions map[string]int\n}\n```\n\n## License\n\n    (The MIT License)\n\n    Copyright (C) 2013-2020 by Ruben Vermeersch \u003cruben@rocketeer.be\u003e\n\n    Permission is hereby granted, free of charge, to any person obtaining a copy\n    of this software and associated documentation files (the \"Software\"), to deal\n    in the Software without restriction, including without limitation the rights\n    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the Software is\n    furnished to do so, subject to the following conditions:\n\n    The above copyright notice and this permission notice shall be included in\n    all copies or substantial portions of the Software.\n\n    THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n    THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubenv%2Fbroadcaster","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frubenv%2Fbroadcaster","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubenv%2Fbroadcaster/lists"}