{"id":26658064,"url":"https://github.com/aburd/broadcaster","last_synced_at":"2026-04-27T12:03:03.388Z","repository":{"id":282817983,"uuid":"949700521","full_name":"aburd/broadcaster","owner":"aburd","description":"A websockets library for deno to help you manage and communicate with websocket connections","archived":false,"fork":false,"pushed_at":"2025-03-17T05:03:30.000Z","size":1,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T05:29:49.472Z","etag":null,"topics":["deno","message","websocket-client","websocket-server","websockets"],"latest_commit_sha":null,"homepage":"https://jsr.io/@aburd/broadcaster","language":"TypeScript","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/aburd.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-17T01:57:25.000Z","updated_at":"2025-03-17T05:03:33.000Z","dependencies_parsed_at":"2025-03-17T05:40:47.211Z","dependency_job_id":null,"html_url":"https://github.com/aburd/broadcaster","commit_stats":null,"previous_names":["aburd/broadcaster"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aburd%2Fbroadcaster","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aburd%2Fbroadcaster/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aburd%2Fbroadcaster/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aburd%2Fbroadcaster/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aburd","download_url":"https://codeload.github.com/aburd/broadcaster/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245431698,"owners_count":20614184,"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":["deno","message","websocket-client","websocket-server","websockets"],"created_at":"2025-03-25T09:17:57.900Z","updated_at":"2026-04-27T12:02:58.354Z","avatar_url":"https://github.com/aburd.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![JSR](https://jsr.io/badges/@aburd/broadcaster)](https://jsr.io/@aburd/broadcaster)\n[![JSR Score](https://jsr.io/badges/@aburd/broadcaster/score)](https://jsr.io/@aburd/broadcaster)\n\n# Broadcaster\n\nAn opinionated deno websockets library to help you manage websocket connections so you can start sending structured messages!\n\n## Summary\n\nWriting WebSocket code with Deno is easy, but writing `myWebsocket.send(arbitraryJsonString)` is anything but simple. In addition, managing websocket connections by yourself is no fun. \n\nBroadcaster gives you the ability to define a few interfaces which tell you how your client/server will communicate over the websocket and then pass in the implementations to a \"connect\" function. From there, your connections will have \"send\" function which ensures the messages you're sending over the socket are typed and handled by the other side of the socket.\n\n## Example\n\n`my-socket-interface.ts`\n\nHere we define the interface of how clients and server will communicate.\n\n- The client can __handle__ a message of:\n  - `\"foo_changed\"`\n  - `\"foo_event\"`\n- The client can __send__ a message of:\n  - `\"update_foo\"`\n  - `\"bar_event\"`\n\nMeanwhile:\n\n- The server can __handle__ a message of:\n  - `\"update_foo\"`\n  - `\"bar_event\"`\n- The server can __send__ a message of:\n  - `\"foo_changed\"`\n  - `\"foo_event\"`\n\n```ts\nimport { ClientHandler, ServerHandler } from \"@aburd/broadcaster\";\nimport { Foo } from \"./types\";\n\n// We define the kind of messages that the client can handle\nexport type ClientHandlers = {\n  \"foo_changed\": ClientHandler\u003c\n    ServerHandlers,\n    { error: string; foo: null } | { foo: Foo; error: null }\n  \u003e;\n  \"foo_event\": ClientHandler\u003cServerHandlers, void\u003e;\n};\n\n// We define the kind of messages that the server can handle\nexport type ServerHandlers = {\n  \"update_foo\": ClientHandler\u003cServerHandlers, { foo: Foo }\u003e;\n  \"bar_event\": ClientHandler\u003cServerHandlers, { baz: string }\u003e;\n};\n```\n\n`my-client.ts`\n\nHere we connect to the WebSocket server from a browser and implement the handlers we defined in our ClientHandlers interface.\n\n```ts\nimport { connect } from \"@aburd/broadcaster\";\nimport { ClientHandlers, ServerHandlers } from \"./my-socket-interface.ts\";\n\nconst socket = await connect\u003cClientHandlers, ServerHandlers\u003e(`${location.origin}/ws`);\nsocket.setHandlersForSocket({\n  \"foo_changed\": (data, socket) =\u003e {\n    // do something with updated data.foo or data.error\n    //\n    socket.send({ key: \"bar_event\", data: { baz: 1 } }); // TypeError - baz needs to be a string\n    socket.send({ key: \"bar\", data: { baz: \"yay\" } }); // OK!\n  },\n  \"foo_event\": (data, socket) =\u003e {\n    // data is null\n  },\n});\n```\n\n`my-server.ts`\n\nHere we setup a basic WebSocket server and implement the handlers we defined in our ServerHandlers interface.\n\n```ts\nimport { createSocketGroup } from \"@aburd/broadcaster\";\nimport { ClientHandlers, ServerHandlers } from \"./my-socket-interface.ts\";\nimport { Foo } from \"./types\";\n\nlet myFoo: Foo = { name: \"my_foo\" }\n\nconst socketGroup = createSocketGroup\u003cServerHandlers, ClientHandlers\u003e()\n\nDeno.serve(async (_req) =\u003e {\n  const url = new URL(req.url);\n\n  if (url.pathname === \"/ws\") {\n    // somehow identify this socket\n    const id = getId(req);\n\n    // will return Response to upgrade the WebSocket\n    const response = socketGroup.addSocket(id, req);\n    socketGroup.setHandlersForSocket(\n      id,\n      {\n        \"update_foo\": (data, key, socketGroup) =\u003e {\n          my_foo = data.foo;\n          // send foo_event to the client who updated foo\n          socketGroup.send(\"foo_event\", { key, data: null })\n\n          // send foo_changed to all sockets in the group\n          socketGroup.broadcast({ key: \"foo_changed\", data: { foo: myFoo } }); // TypeError - baz needs to be a string\n        },\n        \"bar_event\": (data, key, socketGroup) =\u003e {\n          // do something with data.baz\n          if (data.baz === \"bad word\") {\n              socketGroup.removeSocket(key);\n          }\n        },\n      }\n    );\n\n    return response;\n  }\n\n  return new Response(\"Default response\");\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faburd%2Fbroadcaster","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faburd%2Fbroadcaster","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faburd%2Fbroadcaster/lists"}