{"id":21428941,"url":"https://github.com/notcoffee418/websocketmanager","last_synced_at":"2025-03-16T21:42:03.738Z","repository":{"id":197644837,"uuid":"698967432","full_name":"NotCoffee418/websocketmanager","owner":"NotCoffee418","description":"Abstraction on top of gorilla/websocket to manage communication and identification for websockets","archived":false,"fork":false,"pushed_at":"2023-10-13T23:49:57.000Z","size":26,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-23T08:16:14.853Z","etag":null,"topics":["go","golang","gorilla-websocket","websocket-server","websockets"],"latest_commit_sha":null,"homepage":"","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/NotCoffee418.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,"governance":null}},"created_at":"2023-10-01T14:31:36.000Z","updated_at":"2025-01-17T02:26:05.000Z","dependencies_parsed_at":"2023-10-01T22:51:30.510Z","dependency_job_id":null,"html_url":"https://github.com/NotCoffee418/websocketmanager","commit_stats":null,"previous_names":["notcoffee418/websocketmanager"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NotCoffee418%2Fwebsocketmanager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NotCoffee418%2Fwebsocketmanager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NotCoffee418%2Fwebsocketmanager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NotCoffee418%2Fwebsocketmanager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NotCoffee418","download_url":"https://codeload.github.com/NotCoffee418/websocketmanager/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243940060,"owners_count":20372044,"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":["go","golang","gorilla-websocket","websocket-server","websockets"],"created_at":"2024-11-22T22:15:18.071Z","updated_at":"2025-03-16T21:42:03.686Z","avatar_url":"https://github.com/NotCoffee418.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WebSocket Manager\n\nWebSocket Manager is a Go library that simplifies managing WebSocket connections. It offers functionalities like upgrading HTTP connections to WebSocket, managing clients, broadcasting messages, and more.\n\nThis library is an abstraction on top of the [gorilla/websocket](https://github.com/gorilla/websocket) library.\n\n## Installation\n\nInstall the package using `go get`:\n\n```bash\ngo get github.com/NotCoffee418/websocketmanager\n```\n\nAdd the package to your code:\n\n```go\nimport \"github.com/NotCoffee418/websocketmanager\"\n```\n\n## Usage\n\nHere's how to use WebSocket Manager in your Go application.\n\n### Initialize Manager\n\nCreate a new instance of the Manager type using one of the following methods:\n\n```go\n// Create a manager with default settings\nwsManager := websocketmanager.NewDefaultManager()\n\n// Create a manager with customized settings\nwsManager := websocketmanager.NewBuilder().\n              WithReadBufferSize(2048).\n              WithWriteBufferSize(2048).\n              WithClientCleanupDisabled().\n              Build()\n```\n\n### Upgrade HTTP Connection to WebSocket\n\nUpgrade an incoming HTTP request to a WebSocket connection:\n\n```go\nfunc ginHandler(c *gin.Context) {\n\twsClient \u003c- wsManager.UpgradeClient(c.Writer, c.Request)\n\t//...\n}\n```\nThis library is agnostic to the specific Go web framework used, as long as the framework is based on Go's standard `net/http` package.\n\n### Assign Groups\n\nTo categorize clients into groups, use the `AssignGroups` method:\n\n```go\nwsManager.AssignGroups(*client.ConnId, 1, 2)\n```\n\n### Register Observers\n\nTo observe messages from a specific client, register an observer function:\n\n```go\nwsManager.RegisterClientObserver(*client.ConnId, func(wsClient *websocketmanager.Client, messageType int, message []byte) {\n    // Handle incoming message\n})\n```\n\n### Send Messages\n\nTo send messages, you have the following options:\n\n- Broadcast a message to all clients\n  ```go\n  wsManager.BroadcastMessage(messageType, message)\n  ```\n\n- Send a message to a specific group of clients\n  ```go\n  wsManager.SendMessageToGroup(groupID, messageType, message)\n  ```\n\n- Send a message to a specific client\n  ```go\n  wsManager.SendMessageToUser(clientUUID, messageType, message)\n  ```\n\nMessage type definitions can be found in the [gorilla/websocket](https://github.com/gorilla/websocket/blob/666c197fc9157896b57515c3a3326c3f8c8319fe/conn.go#L63) library.\n```go\nmessageType := websocket.TextMessage\n```\n\n### Manage Clients\n\nYou can manually unregister clients or retrieve specific clients by their UUID:\n\n- To unregister a client:\n  ```go\n  wsManager.Unregister(clientUUID)\n  ```\n\n- To get a specific client:\n  ```go\n  client, err := wsManager.GetWebSocketClient(clientUUID)\n  ```\n\n### Cleanup\n\nBy default, WebSocket Manager will automatically clean up inactive clients. You can disable this during the initialization step if needed.\n\n## Contributing\n\nContributions are welcome. Feel free to open a pull request or issue on GitHub.\n\n## License\n\nThis project is licensed under the MIT License.\n\nFor more information, please refer to the [LICENSE](LICENSE) file in the repository.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnotcoffee418%2Fwebsocketmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnotcoffee418%2Fwebsocketmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnotcoffee418%2Fwebsocketmanager/lists"}