{"id":21586422,"url":"https://github.com/gobackpack/websocket","last_synced_at":"2025-03-18T09:16:37.607Z","repository":{"id":40552473,"uuid":"360086419","full_name":"gobackpack/websocket","owner":"gobackpack","description":"WIP: Websockets hub","archived":false,"fork":false,"pushed_at":"2023-09-15T22:14:27.000Z","size":105,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-24T15:36:30.520Z","etag":null,"topics":["golang","websockets"],"latest_commit_sha":null,"homepage":"","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/gobackpack.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-21T08:17:53.000Z","updated_at":"2023-09-16T18:50:39.000Z","dependencies_parsed_at":"2024-06-21T02:10:21.576Z","dependency_job_id":"432c3afe-0737-4801-9680-b0639038f953","html_url":"https://github.com/gobackpack/websocket","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/gobackpack%2Fwebsocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobackpack%2Fwebsocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobackpack%2Fwebsocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobackpack%2Fwebsocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gobackpack","download_url":"https://codeload.github.com/gobackpack/websocket/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244189824,"owners_count":20412991,"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":["golang","websockets"],"created_at":"2024-11-24T15:13:38.723Z","updated_at":"2025-03-18T09:16:37.585Z","avatar_url":"https://github.com/gobackpack.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"![alt Go](https://img.shields.io/github/go-mod/go-version/gobackpack/websocket)\n\nWork in progress...\n\nUsage\n\n```go\n// initialize hub and start listening for connections\nhub := websocket.NewHub()\n\nhubCtx, hubCancel := context.WithCancel(context.Background())\nhubFinished := hub.ListenForConnections(hubCtx)\n\n// create client and establish connection with ws hub\nclient, err := hub.EstablishGorillaWsConnection(c.Writer, c.Request, groupId, connId)\nif err != nil {\n    logrus.Errorf(\"failed to establish connection with groupId -\u003e %s: %s\", groupId, err)\n    return\n}\n\nclientCtx, clientCancel := context.WithCancel(hubCtx)\nclientFinished := client.ReadMessages(clientCtx)\n\n// handle messages\ngo func (clientCancel context.CancelFunc, client *websocket.Client) {\n    defer clientCancel()\n\n    for {\n        select {\n        case msg := \u003c-client.OnMessage:\n            logrus.Infof(\"client %s received message: %s\", client.ConnectionId, msg)\n            go hub.SendToGroup(groupId, msg)\n        case err := \u003c-client.OnError:\n            logrus.Errorf(\"client %s received error: %s\", client.ConnectionId, err)\n        case err = \u003c-client.LostConnection:\n            hub.DisconnectFromGroup(client.GroupId, client.ConnectionId)\n            return\n        }\n    }\n}(clientCancel, client)\n\n\u003c-clientFinished\n\n// send message\ngo hub.SendToGroup(groupId, []byte(\"message to group\"))\n\ngo hub.SendToAllGroups([]byte(\"message to all groups\"))\n\ngo hub.SendToConnectionId(groupId, connectionId, []byte(\"message to connection\"))\n\ngo hub.SendToOthersInGroup(groupId, client.ConnectionId, []byte(\"message to all connections from my group except myself\"))\n\n// disconnect\nhub.DisconnectFromGroup(groupId, connectionId)\n\n// close\nhubCancel()\n\u003c-hubFinished\n```\n\n#### Todo\n\n* Make sure the following are thread-safe:\n\n```go\nclient.read()\nclient.write()\nhub.Groups\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgobackpack%2Fwebsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgobackpack%2Fwebsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgobackpack%2Fwebsocket/lists"}