{"id":18335652,"url":"https://github.com/chanlito/socket.io-controllers","last_synced_at":"2026-04-22T21:33:41.257Z","repository":{"id":57365150,"uuid":"100691955","full_name":"chanlito/socket.io-controllers","owner":"chanlito","description":"Use ES6 Class with Socket.IO","archived":false,"fork":false,"pushed_at":"2017-08-18T09:52:02.000Z","size":17,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-30T21:45:48.070Z","etag":null,"topics":["es6-classes","socket-io","socket-programming"],"latest_commit_sha":null,"homepage":null,"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/chanlito.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}},"created_at":"2017-08-18T08:41:59.000Z","updated_at":"2023-11-29T11:28:25.000Z","dependencies_parsed_at":"2022-09-16T20:01:29.412Z","dependency_job_id":null,"html_url":"https://github.com/chanlito/socket.io-controllers","commit_stats":null,"previous_names":["brucehem/socket.io-controllers"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/chanlito/socket.io-controllers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanlito%2Fsocket.io-controllers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanlito%2Fsocket.io-controllers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanlito%2Fsocket.io-controllers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanlito%2Fsocket.io-controllers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chanlito","download_url":"https://codeload.github.com/chanlito/socket.io-controllers/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanlito%2Fsocket.io-controllers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32079517,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-21T02:38:07.213Z","status":"ssl_error","status_checked_at":"2026-04-21T02:38:06.559Z","response_time":128,"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":["es6-classes","socket-io","socket-programming"],"created_at":"2024-11-05T20:03:45.097Z","updated_at":"2026-04-21T06:01:24.293Z","avatar_url":"https://github.com/chanlito.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# socket.io-controllers\n\n## Description\n\nUse ES6 Class with Socket.IO\n\n## Installation\n\n```bash\n$ npm install socket.io-controllers --save\n```\n\n## Usage\n\nLet's say that our application will be using Koa and Socket.IO.\n\n- app.js\n\n```javascript\n\nconst http = require('http');\nconst path = require('path');\nconst Koa = require('koa');\nconst SocketIO = require('socket.io');\nconst { setupSocketIOControllers } = require('socket.io-controllers');\n\nconst app = new Koa();\nconst server = http.createServer(app.callback());\n\napp.context.io = setupSocketIOControllers({\n\tserver,\n\tcontrollers: {\n\t\tdir: path.resolve(__dirname, 'socket-controllers')\n\t\t// suffix: '.sc.js' // Default: .socket-controller.\n\t}\n});\n\nserver.listen(3000, () =\u003e console.log('Server running on port 3000'));\n\n```\nCreate a socket-controllers folder (inside we will define our Socket Controller Classes)\n- socket-controllers/default.socket-controller.js\n```javascript\nconst { SocketController } = require('socket.io-controllers');\n\nclass DefaultSocketController extends SocketController {\n\t/**\n\t * The \"use\" function will be our socket middleware.\n\t * \n\t * Call this.next() anytime to continue.\n\t * \n\t * Or call this.next(new Error('some error')) to deny access.\n\t */\n\tuse() {\n\t\tconst { socket, next } = this;\n\t\tconst { authToken } = socket.handshake.query;\n\n\t\t// Here we can do some check with the token\n\t\tif (authToken === '123456') {\n\t\t\tthis.next();\n\t\t} else {\n\t\t\tthis.next(new Error('Authentication Failed.'));\n\t\t}\n\t}\n\n\t/**\n\t * We define our socket event handler like the following.\n\t * \n\t * On client side for example:\n\t * \n\t * socket.emit('SomeCustomEvent', payload)\n\t * \n\t * or with acknowledgement\n\t * \n\t * socket.emit('SomeCustomEvent', payload, ack =\u003e console.log(ack))\n\t *\n\t * You have access to socket.io server (io) and current socket (socket)\n\t */\n\tonSomeCustomEvent(payload) {\n\t\tconst { io, socket } = this;\n\t}\n\n\tonSomeCustomEvent2(payload, fn) {}\n}\n\nexports.DefaultSocketController = DefaultSocketController;\n\n\n```\nWhat about namespacing?\n```javascript\nclass DefaultSocketController extends SocketController {} // io.of('/')\n\nclass AuthSocketController extends SocketController {} // io.of('/auth')\n\nclass ChatSocketController extends SocketController {} // io.of('/chat')\n```\n\n## LICENSE\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchanlito%2Fsocket.io-controllers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchanlito%2Fsocket.io-controllers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchanlito%2Fsocket.io-controllers/lists"}