{"id":21482368,"url":"https://github.com/topgunbuild/socket","last_synced_at":"2026-05-20T03:31:16.556Z","repository":{"id":149902733,"uuid":"619250181","full_name":"TopGunBuild/socket","owner":"TopGunBuild","description":"Scalable realtime pub/sub and RPC serverless framework","archived":false,"fork":false,"pushed_at":"2023-11-22T06:44:00.000Z","size":1787,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-30T12:39:18.633Z","etag":null,"topics":["nodejs","pubsub","socketcluster","typescript","websocket"],"latest_commit_sha":null,"homepage":"","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/TopGunBuild.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":"2023-03-26T17:47:02.000Z","updated_at":"2023-08-23T05:16:03.000Z","dependencies_parsed_at":"2023-11-22T07:31:23.704Z","dependency_job_id":"74b2b6ac-4d9b-4fae-a05f-c7d7c0f0cdab","html_url":"https://github.com/TopGunBuild/socket","commit_stats":null,"previous_names":["topgunjs/topgun-socket","topgunbuild/socket","topgunbuild/topgun-socket"],"tags_count":41,"template":false,"template_full_name":null,"purl":"pkg:github/TopGunBuild/socket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TopGunBuild%2Fsocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TopGunBuild%2Fsocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TopGunBuild%2Fsocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TopGunBuild%2Fsocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TopGunBuild","download_url":"https://codeload.github.com/TopGunBuild/socket/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TopGunBuild%2Fsocket/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33244688,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-20T03:30:51.439Z","status":"ssl_error","status_checked_at":"2026-05-20T03:30:49.443Z","response_time":356,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["nodejs","pubsub","socketcluster","typescript","websocket"],"created_at":"2024-11-23T12:32:49.130Z","updated_at":"2026-05-20T03:31:16.542Z","avatar_url":"https://github.com/TopGunBuild.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\" style=\"border-bottom: none;\"\u003e🚀 @topgunbuild/socket\u003c/h1\u003e\n\u003ch3 align=\"center\"\u003ePub/sub and RPC toolkit for \u003ca href=\"https://github.com/TopGunBuild/topgun\"\u003eTopGun\u003c/a\u003e\u003c/h3\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://npm.im/@topgunbuild/socket\"\u003e\n    \u003cimg alt=\"npm\" src=\"https://badgen.net/npm/v/@topgunbuild/socket\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://bundlephobia.com/result?p=@topgunbuild/socket\"\u003e\n    \u003cimg alt=\"bundlephobia\" src=\"https://img.shields.io/bundlephobia/minzip/@topgunbuild/socket.svg\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://opensource.org/licenses/MIT\"\u003e\n      \u003cimg alt=\"License: MIT\" src=\"https://img.shields.io/badge/License-MIT-yellow.svg\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://snyk.io/test/github/TopGunBuild/topgun\"\u003e\n        \u003cimg alt=\"Snyk\" src=\"https://snyk.io/test/github/TopGunBuild/topgun/badge.svg\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n## Client Setting up\n\nTo install this module:\n\n```bash\nnpm install @topgunbuild/socket\n```\n\n## How to use client module\n\nThe socket-client script is called `client.js`(ESM) or `client.global.js`(iife) (located in the dist directory).\nEmbed it in your HTML page like this:\n\n```html\n\u003cscript type=\"text/javascript\" src=\"https://unpkg.com/@topgunbuild/socket@latest/dist/client.global.js\"\u003e\u003c/script\u003e\n```\n\n\\* Note that the src attribute may be different depending on how you setup your HTTP server.\n\nOnce you have embedded the client `client.global.js` into your page, you will gain access to a global `TopGunSocket` object.\nYou may also use CommonJS `require` or ES6 module imports.\n\n### Connect to a server\n\n```js\nlet socket = TopGunSocket.create({\n    hostname: \"localhost\",\n    port: 8000,\n});\n```\n\n### Transmit data\n\n```js\n// Transmit some data to the server.\n// It does not expect a response from the server.\n// From the server socket, it can be handled using either:\n// - for await (let data of socket.receiver('foo')) {}\n// - let data = await socket.receiver('foo').once()\nsocket.transmit('foo', 123);\n```\n\n### Invoke an RPC\n\n```js\n(async () =\u003e {\n\n  // Invoke an RPC on the server.\n  // It expects a response from the server.\n  // From the server socket, it can be handled using either:\n  // - for await (let req of socket.procedure('myProc')) {}\n  // - let req = await socket.procedure('myProc').once()\n  let result = await socket.invoke('myProc', 123);\n\n})();\n```\n\n### Subscribe to a channel\n\n```js\n(async () =\u003e {\n\n  // Subscribe to a channel.\n  let myChannel = socket.subscribe('myChannel');\n\n  await myChannel.listener('subscribe').once();\n  // myChannel.state is now 'subscribed'.\n\n})();\n```\n\n### Get a channel without subscribing\n\n```js\n(async () =\u003e {\n\n  let myChannel = socket.channel('myChannel');\n\n  // Can subscribe to the channel later as a separate step.\n  myChannel.subscribe();\n  await myChannel.listener('subscribe').once();\n  // myChannel.state is now 'subscribed'.\n\n})();\n```\n\n### Publish data to a channel\n\n```js\n// Publish data to the channel.\nmyChannel.publish('This is a message');\n\n// Publish data to the channel from the socket.\nsocket.publish('myChannel', 'This is a message');\n```\n\n### Consume data from a channel\n\n```js\n(async () =\u003e {\n\n  for await (let data of myChannel) {\n    // ...\n  }\n\n})();\n```\n\n### Connect over HTTPS:\n\n```js\nlet options = {\n  hostname: 'securedomain.com',\n  secure: true,\n  port: 443,\n  rejectUnauthorized: false // Only necessary during debug if using a self-signed certificate\n};\n// Initiate the connection to the server\nlet socket = TopGunSocket.create(options);\n```\n\n### Connect Options\n\n```js\nlet options = {\n  path: '/topgunsocket/',\n  port: 8000,\n  hostname: '127.0.0.1',\n  autoConnect: true,\n  secure: false,\n  rejectUnauthorized: false,\n  connectTimeout: 10000, //milliseconds\n  ackTimeout: 10000, //milliseconds\n  channelPrefix: null,\n  disconnectOnUnload: true,\n  multiplex: true,\n  autoReconnectOptions: {\n    initialDelay: 10000, //milliseconds\n    randomness: 10000, //milliseconds\n    multiplier: 1.5, //decimal\n    maxDelay: 60000 //milliseconds\n  },\n  authEngine: null,\n  codecEngine: null,\n  subscriptionRetryOptions: {},\n  query: {\n    yourparam: 'hello'\n  }\n};\n```\n\nFor more detailed examples of how to use @topgunbuild/socket, see `test` folder.\n\n## How to use server module\n\nYou need to attach it to an existing Node.js http or https server (example):\n```js\nconst http = require('http');\nconst topGunSocketServer = require(\"@topgunbuild/socket/server\");\n\nlet httpServer = http.createServer();\nlet agServer = topGunSocketServer.attach(httpServer);\n\n(async () =\u003e {\n  // Handle new inbound sockets.\n  for await (let {socket} of agServer.listener('connection')) {\n\n    (async () =\u003e {\n      // Set up a loop to handle and respond to RPCs for a procedure.\n      for await (let req of socket.procedure('customProc')) {\n        if (req.data.bad) {\n          let error = new Error('Server failed to execute the procedure');\n          error.name = 'BadCustomError';\n          req.error(error);\n        } else {\n          req.end('Success');\n        }\n      }\n    })();\n\n    (async () =\u003e {\n      // Set up a loop to handle remote transmitted events.\n      for await (let data of socket.receiver('customRemoteEvent')) {\n        // ...\n      }\n    })();\n\n  }\n})();\n\nhttpServer.listen(8000);\n```\n\n## Running the tests\n\n-   Clone this repo: `git clone git@github.com:TopGunBuild/@topgunbuild/socket.git`\n-   Navigate to project directory: `cd @topgunbuild/socket`\n-   Install all dependencies: `npm install`\n-   Run the tests: `npm test`\n\n## Benefits of async `Iterable` over `EventEmitter`\n\n-   **More readable**: Code is written sequentially from top to bottom. It avoids event handler callback hell. It's also much easier to write and read complex integration test scenarios.\n-   **More succinct**: Event streams can be easily chained, filtered and combined using a declarative syntax (e.g. using async generators).\n-   **More manageable**: No need to remember to unbind listeners with `removeListener(...)`; just `break` out of the `for-await-of` loop to stop consuming. This also encourages a more declarative style of coding which reduces the likelihood of memory leaks and unintended side effects.\n-   **Less error-prone**: Each event/RPC/message can be processed sequentially in the same order that they were sent without missing any data; even if asynchronous calls are made inside middleware or listeners. On the other hand, with `EventEmitter`, the listener function for the same event cannot be prevented from running multiple times in parallel; also, asynchronous calls within middleware and listeners can affect the final order of actions; all this can cause unintended side effects.\n\n## Reference\n\nThe code in this repository is based on `socketcluster-server` (https://github.com/SocketCluster/socketcluster-server) and `socketcluster-client` (https://github.com/SocketCluster/socketcluster-client). The code is written in typescript and is ready to run in a serverless environment.\n\n## License\n\n(The MIT License)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftopgunbuild%2Fsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftopgunbuild%2Fsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftopgunbuild%2Fsocket/lists"}