{"id":16844763,"url":"https://github.com/stephanhoyer/sock-channels","last_synced_at":"2025-03-22T05:31:20.600Z","repository":{"id":14436072,"uuid":"17147447","full_name":"StephanHoyer/sock-channels","owner":"StephanHoyer","description":"Allows to create channels on top of socket connection based on sock.js.","archived":false,"fork":false,"pushed_at":"2025-01-22T17:23:12.000Z","size":62,"stargazers_count":1,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T08:48:12.017Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"sofa-framework/sofa","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StephanHoyer.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":"2014-02-24T19:14:36.000Z","updated_at":"2022-03-27T10:11:40.000Z","dependencies_parsed_at":"2024-02-08T16:52:57.972Z","dependency_job_id":"48cf862c-b199-4bae-9324-3f9dfc9b1560","html_url":"https://github.com/StephanHoyer/sock-channels","commit_stats":{"total_commits":46,"total_committers":3,"mean_commits":"15.333333333333334","dds":"0.10869565217391308","last_synced_commit":"c449a052c624d87a9f7f7b2f441e885f92d3f0d6"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephanHoyer%2Fsock-channels","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephanHoyer%2Fsock-channels/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephanHoyer%2Fsock-channels/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephanHoyer%2Fsock-channels/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StephanHoyer","download_url":"https://codeload.github.com/StephanHoyer/sock-channels/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244912800,"owners_count":20530764,"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-13T12:56:32.243Z","updated_at":"2025-03-22T05:31:20.180Z","avatar_url":"https://github.com/StephanHoyer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/StephanHoyer/sock-channels.svg?branch=master)](https://travis-ci.org/StephanHoyer/sock-channels)\n[![Dependency Status](https://david-dm.org/StephanHoyer/sock-channels.svg)](https://david-dm.org/StephanHoyer/sock-channels)\n[![rethink.js](https://img.shields.io/badge/rethink-js-yellow.svg)](https://github.com/rethinkjs/manifest)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)\n\nsock-channels\n=============\n\nAllows to create channels on top of socket connection based on\n[sock.js](http://sockjs.org).\n\nInstallation\n------------\n\n    npm install sock-channels\n\nBasic usage\n-----------\n\nUsage is really simple. Just create an sock.js connection on client and server\nside and create a channel with the same socket connection and the same prefix.\n\nEstablish a connection on the client\n\n```javascript\nvar SockjsClient = require('sockjs-client')\nvar createClientChannel = require('sock-channels')\n\nclientRootChannel = createClientChannel(new SockjsClient('/ws'))\n```\n\nOn the server side simply do\n\n```javascript\nvar http = require('http')\nvar sockjsServer = require('sockjs')\nvar createServerChannel = require('sock-channels')\n\nsocketServer = sockjsServer.createServer()\nvar httpServer = http.createServer()\nsocketServer.installHandlers(httpServer, {prefix: '/ws'})\nhttpServer.listen(9999, '0.0.0.0')\nserverRootChannel = createServerChannel(socketServer)\n```\n\nIn both cases, there has to be an active Sock.js connection on the `/ws`\nroute.\n\nYou know have one root channel to communitcate through. Objects are serialized to JSON.\n\n```javascript\n//client -\u003e server\nclientRootChannel.write({my: 'things', are: [1,2]});\n\n//server -\u003e all clients (broadcast)\nserverRootChannel.write({my: 'things', are: [1,2]});\n\n//server -\u003e specific client\nserverRootChannel.onConnect.push(function (connection) {\n  serverRootChannel.write({my: 'things', are: [1,2]}, { only: connection });\n  // ... or\n  connection.write(serverRootChannel, {my: 'things', are: [1,2]});\n})\n\n//server -\u003e all but specific client\nserverRootChannel.onConnect.push(function (connection) {\n  serverRootChannel.write({my: 'things', are: [1,2]}, { exclude: connection });\n})\n```\n\nListening on data also is really easy\n\n```javascript\n//client\nchannel.onData.push(function(data) {\n  data.my == 'things' // true\n});\n\n//server\nchannel.onData.push(function(data, connection) {\n  data.my == 'things' // true\n}\n// ... or\nchannel.onData.push(function(data, channel) {\n  data.my == 'things' // true\n  channel.id == 'root' // true\n}\n```\n\nNote that you there are also two possibilities to listen on data on server side,\none is channel centric the other connection related. You always get the other\nobject as parameter in the callback function.\n\nThis hopefully offers great flexibility in using this module.\n\nChannel multiplexing\n--------------------\n\nIt's the same on client and on server side:\n\n```javascript\nvar fooCh = existingChannel.sub('foo');\nvar barCh = fooCh.sub('bar');\n```\n\nThis can be done to any depth you want. Internaly sock-channels store the\nchannels by concatenating the channel id's together:\n\n```javascript\nfooCh.id == 'root:foo'; //true\nbarCh.id == 'root:foo:bar'; //true\n```\n\nAll messages are marked according the channel id, but you don't have to care\nabout this.\n\nEvents\n------\n\nEvents are done with a minimal event-observer lib called [smoke-signal](https://github.com/StephanHoyer/smoke-signal)\n\nyou can attach to events by calling `once` or `push`. Detach with `pause`.\n\nOn the client channel there are the following events\n\n* `channel.onOpen`: calles if the connection to the server is established\n* `channel.onData`: calles if there is some incomming data, `data` is first argument\n\nOn the server channel there are the following events\n\n* `channel.onConnect`: calles if the connection to the client is established, `connection` is first argument\n* `channel.onData`: calles if there is some incomming data, `data` is first argument, `connection` is second\n\nOn the server connection there are the following events\n* `channel.onData`: calles if there is some incomming data, `data` is first * argument, `channel` is second\n\nLicense\n-------\n\n(The MIT License)\n\nCopyright (c) 2016 Stephan Hoyer \u003cste.hoyer@gmail.com\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the 'Software'), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstephanhoyer%2Fsock-channels","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstephanhoyer%2Fsock-channels","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstephanhoyer%2Fsock-channels/lists"}