{"id":23053365,"url":"https://github.com/component/channel","last_synced_at":"2025-08-15T03:32:37.645Z","repository":{"id":13828920,"uuid":"16525619","full_name":"component/channel","owner":"component","description":"two-sided event emitter with support for middleware","archived":false,"fork":false,"pushed_at":"2014-02-13T02:22:12.000Z","size":204,"stargazers_count":19,"open_issues_count":0,"forks_count":2,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-12-04T10:04:35.676Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/component.png","metadata":{"files":{"readme":"Readme.md","changelog":"History.md","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":"2014-02-04T21:06:44.000Z","updated_at":"2023-08-17T22:32:14.000Z","dependencies_parsed_at":"2022-09-13T17:12:58.815Z","dependency_job_id":null,"html_url":"https://github.com/component/channel","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/component%2Fchannel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/component%2Fchannel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/component%2Fchannel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/component%2Fchannel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/component","download_url":"https://codeload.github.com/component/channel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229786916,"owners_count":18123998,"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-12-16T00:17:49.253Z","updated_at":"2024-12-16T00:17:49.793Z","avatar_url":"https://github.com/component.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# channel\n\n\u003e Event comes in here, event comes out there.\n\n![](https://i.cloudup.com/fXcoOto61r.png)\n\nA **channel** is a two-sided event emitter with support for middleware. If you emit an event on the `a` side it will come out on the `b` side, and vice versa.\n\n## Basic Usage\n\n```javascript\nvar chan = channel();\nchan.b.on('foo', function(thing) { console.log(thing) });\nchan.a.emit('foo', 'thing');\n```\n\nOutputs:\n\n```\nthing\n```\n\n## Middleware and Data Transformation\n\nChannels can operate on the data being transmitted through express-like middleware. \n\n```javascript\nvar chan = channel();\n\n// uppercase middleware\nchan.use(function(evt, next) {\n  evt.args = evt.args.map(function(str) { return str.toUpperCase() });\n  next();\n});\n\nchan.b.on('foo', function(thing) { console.log(thing) });\nchan.a.emit('foo', 'thing');\n```\n\nOutputs:\n\n```\nTHING\n```\n\nMiddleware are executed asynchronously, so you can do interesting things like introducing latency or dropping events to simulate\nadverse network conditions locally.\n\n![](https://i.cloudup.com/RT8a5tfItX.png)\n\n### Middleware parameters\n\n#### The `evt` middleware parameter\n\n`evt` contains the following properties:\n\n* `source`: the source endpoint. you can check if the event came from `a` or `b`, by comparing this value with `this.a` and `this.b` inside the middleware.\n* `name`: the event name\n* `args`: the event args\n\n#### The `next` middleware parameter\n\nJust call `next()` to propagate the event to the next middleware in the chain, or to emit the event if you're the last middleware.\n\nTo drop an event, simply return without calling `next()`.\n\n### Applications of Middleware\n\nHere are some applications of channel middleware:\n\n* Simulating latency ([component/channel-latency](https://github.com/component/channel-latency))\n* Simulating packet loss ([component/channel-drop](https://github.com/component/channel-drop))\n* Debouncing events ([component/channel-debounce](https://github.com/component/channel-debounce))\n* Batching events\n* Filtering events\n* Encrypting/decrypting data\n* Encoding/decoding data\n* Compressing/decompressing data\n\n## Piping in and out\n\nYou can use the `pipeIn()` and `pipeOut()` methods in the `a` and `b` endpoints to easily connect channels to event emitters, other channels or to something like [socket.io](https://socket.io).\n\nThis is useful, for example, for fanning in events from multiple sources into a single event emitter.\n\nThe `pipe()` method is also available when you want to simultaneously pipe events in and out.\n\n## Installation\n\n  Install with [component(1)](http://component.io):\n\n    $ component install component/channel\n\n## API\n\n### `channel()`\n\nCreates a new `Channel`.\n\n### Channel\n\n#### `Channel#a`\n\nThe `a` `Endpoint` of the channel.\n\n#### `Channel#b`\n\nThe `b` `Endpoint` of the channel.\n\n#### `Channel#use(middleware)`\n\nUse a middleware on this channel. `middleware` is a function, taking two parameters: `evt` and `next`.\n\n### Endpoint\n\n#### `Endpoint#emit(name, args...)`\n\nEmit an event with `name` and `args`. (It will fire on the other `Endpoint`, after going through the middleware);\n\n#### `Endpoint#on(name, fn)`\n\nRegister `fn` as a handler function for event `name`.\n\n#### `Endpoint#pipeOut(name, target)`\n\nPipe out `name` events into `target`. Target can be an Emitter, another Channel or anything that exposes an `emit()` method.\n\n#### `Endpoint#pipeIn(name, target)`\n\nPipe in `name` events from `target`. Target can be an Emitter, another Channel or anything that exposes an `on()` method.\n\n#### `Endpoint#pipe(name, target)`\n\nEquivalent to calling both `pipeOut()` and `pipeIn()`.\n\n## License\n\n  The MIT License (MIT)\n\n  Copyright (c) 2014 Automattic, Inc.\n\n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files (the \"Software\"), to deal\n  in the Software without restriction, including without limitation the rights\n  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n  copies of the Software, and to permit persons to whom the Software is\n  furnished to do so, subject to the following conditions:\n\n  The above copyright notice and this permission notice shall be included in\n  all copies or substantial portions of the Software.\n\n  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n  THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcomponent%2Fchannel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcomponent%2Fchannel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcomponent%2Fchannel/lists"}