{"id":17166625,"url":"https://github.com/willcrichton/easyws","last_synced_at":"2025-03-24T18:15:33.365Z","repository":{"id":141492965,"uuid":"10343602","full_name":"willcrichton/easyws","owner":"willcrichton","description":"Super-abstracted API for websockets in Go","archived":false,"fork":false,"pushed_at":"2013-06-01T05:33:32.000Z","size":140,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-29T22:46:29.399Z","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/willcrichton.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":"2013-05-28T19:29:07.000Z","updated_at":"2013-12-07T13:14:36.000Z","dependencies_parsed_at":"2023-03-13T10:33:13.169Z","dependency_job_id":null,"html_url":"https://github.com/willcrichton/easyws","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/willcrichton%2Feasyws","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willcrichton%2Feasyws/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willcrichton%2Feasyws/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willcrichton%2Feasyws/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willcrichton","download_url":"https://codeload.github.com/willcrichton/easyws/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245325221,"owners_count":20596818,"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-10-14T23:06:10.470Z","updated_at":"2025-03-24T18:15:33.339Z","avatar_url":"https://github.com/willcrichton.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"easyws\n======\n\nSuper-abstracted API for WebSockets in Go. A generalized, reusable form of the WebSocket server implemented by [Gary Burd](http://gary.beagledreams.com/page/go-websocket-chat.html).\n\n### The API\n\nThe `easyws` package contains exactly one exported function: \n```go\neasyws.Socket(path string, msgHandle func(string, *easyws.Connection, *Hub), \n\t\t\t\t\t\t   joinHandle func(*http.Request, *easyws.Connection, *easyws.Hub),\n\t\t\t\t\t\t   leaveHandle func(*http.Request, *easyws.Connection, *easyws.Hub))\n```\n\nThe Socket function creates a new WebSocket at the given path and accepts three handlers: `msgHandle` for when a message is sent to the WebSocket, `joinHandle` for when a user connects to the WebSocket, and `leaveHandle` for when a user disconnects or the client otherwise closes his socket connection. Both handlers are called with the corresponding `Connection` and `Hub`. Those datatypes are as follows:\n\n```go\ntype Connection struct {\n    ws   *websocket.Conn\n    send chan string\n    h    *Hub\n}\n\ntype Hub struct {\n    connections  map[*Connection]bool\n    receiver     chan msginfo\n    register     chan *Connection\n    unregister   chan *Connection\n    onjoin       func(*http.Request, *Connection, *Hub)\n    onleave      func(*http.Request, *Connection, *Hub)\n}\n```\n\nA `Connection` corresponds to a single connection on the WebSocket, and a `Hub` is a structure which holds data about a WebSocket. Note that a `Connection` struct holds another struct from the `websocket` package, which you can read more about [here](https://code.google.com/p/go/source/browse/websocket/websocket.go?repo=net).\n\n### Example\n\nLuckily, using `easyws` is, as the name implies, easy! Here's a short example:\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"github.com/willcrichton/easyws\"\n)\n\nfunc wsOnMessage(msg string, c *easyws.Connection, h *easyws.Hub) {\n    fmt.Println(\"Received message: \" + msg)\n}\n\nfunc wsOnJoin(r *http.Request, c *easyws.Connection, h *easyws.Hub) {\n    fmt.Println(\"New user connected\")\n}\n\nfunc wsOnLeave(r *http.Request, c *easyws.Connection, h *easyws.Hub) {\n\tfmt.Println(\"User left websocket\")\n}\n\nfunc main(){\n    easyws.Socket(\"/ws\", wsOnMessage, wsOnJoin, wsOnLeave)\n    log.Fatal(http.ListenAndServe(\":8080\", nil))\n}\n```\n\nIn the above example, once your Go executable is running, you could then connect to your WebSocket in Javascript as follows:\n\n```javascript\nws = new WebSocket(\"ws://localhost:8080/ws\");\nws.send(\"I AM A MESSAGE\");\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillcrichton%2Feasyws","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillcrichton%2Feasyws","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillcrichton%2Feasyws/lists"}