{"id":13809005,"url":"https://github.com/chrisguttandin/rxjs-broker","last_synced_at":"2026-03-17T04:05:47.323Z","repository":{"id":44847171,"uuid":"60128529","full_name":"chrisguttandin/rxjs-broker","owner":"chrisguttandin","description":"An RxJS message broker for WebRTC DataChannels and WebSockets.","archived":false,"fork":false,"pushed_at":"2024-10-16T13:39:06.000Z","size":15133,"stargazers_count":24,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-24T11:34:29.936Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/chrisguttandin.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":"2016-05-31T22:41:50.000Z","updated_at":"2024-10-16T13:39:12.000Z","dependencies_parsed_at":"2023-02-16T21:00:44.552Z","dependency_job_id":"ea3c13a6-e633-46c6-aa38-de9ea809010d","html_url":"https://github.com/chrisguttandin/rxjs-broker","commit_stats":{"total_commits":3304,"total_committers":4,"mean_commits":826.0,"dds":0.04328087167070216,"last_synced_commit":"7df1cb426055e5f7f87e4624ed484ede6deec742"},"previous_names":[],"tags_count":304,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisguttandin%2Frxjs-broker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisguttandin%2Frxjs-broker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisguttandin%2Frxjs-broker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisguttandin%2Frxjs-broker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrisguttandin","download_url":"https://codeload.github.com/chrisguttandin/rxjs-broker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232945589,"owners_count":18600685,"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-08-04T01:01:57.514Z","updated_at":"2026-03-17T04:05:45.849Z","avatar_url":"https://github.com/chrisguttandin.png","language":"JavaScript","funding_links":[],"categories":["Underlying Technologies"],"sub_categories":["RxJS"],"readme":"# rxjs-broker\n\n**An RxJS message broker for WebRTC DataChannels and WebSockets.**\n\n[![version](https://img.shields.io/npm/v/rxjs-broker.svg?style=flat-square)](https://www.npmjs.com/package/rxjs-broker)\n\nThis module is using the power of [RxJS](https://rxjs.dev) to wrap WebSockets or WebRTC DataChannels. It returns a [Subject](https://rxjs.dev/api/index/class/Subject) which can be used with all the operators that RxJS provides. But it also provides some additional functionality.\n\n## Usage\n\nTo install `rxjs-broker` via [npm](https://www.npmjs.com/package/rxjs-broker) you can run the following command.\n\n```shell\nnpm install rxjs-broker\n```\n\n`rxjs-broker` does provide two utility functions: `connect()` and `wrap()`. If you're using ES2015 modules you can import them like that.\n\n```js\nimport { connect, wrap } from 'rxjs-broker';\n```\n\n### connect(url: string, subjectConfig?: { openObserver?: NextObserver\\\u003cvoid\u003e }): WebSocketSubject\n\nThe `connect()` function takes a URL as a parameter and returns a `WebSocketSubject` which extends the `AnonymousSubject` provided by RxJS. It also implements the `IRemoteSubject` interface which adds two additional methods. It gets explained in more detail below.\n\n```js\nconst webSocketSubject = connect('wss://super-cool-websock.et');\n```\n\nThe second parameter can be used to specify an `openObserver` which works similar to the [`openObserver` of the `WebSocketSubject` provided by RxJS](https://rxjs-dev.firebaseapp.com/api/webSocket/WebSocketSubjectConfig#openObserver). The `next()` method of it gets called when the underlying WebSocket emits an open event.\n\n### wrap(dataChannel: DataChannel, subjectConfig?: { openObserver?: NextObserver\\\u003cvoid\u003e }): DataChannelSubject\n\nThe `wrap()` function can be used to turn a WebRTC DataChannel into a `DataChannelSubject` which does also extend the `AnonymousSubject` and implements the `IRemoteSubject` interface.\n\n```js\n// Let's imagine a variable called dataChannel exists and its value is a WebRTC DataChannel.\nconst dataChannelSubject = wrap(dataChannel);\n```\n\nThe second parameter can be used to specify an `openObserver`. The `next()` method of it gets called when the underlying DataChannel emits an open event.\n\n### IRemoteSubject\n\nAs mentioned above the `IRemoteSubject` interface is used to describe the common behavior of the `DataChannelSubject` and the `WebSocketSubject`. In TypeScript it looks like this:\n\n```typescript\ninterface IRemoteSubject\u003cT\u003e {\n    close(): void;\n\n    send(message: T): Promise\u003cvoid\u003e;\n}\n```\n\n#### close()\n\nThe `close()` method is meant to close the underlying WebSocket or WebRTC DataChannel.\n\n#### send(message): Promise\u003cvoid\u003e\n\nThe `send()` method is a supercharged version of `next()`. It will stringify a given JSON message before sending it and returns a `Promise` which resolves when the message is actually on it's way.\n\n### mask(mask, maskableSubject): IRemoteSubject\n\n`rxjs-broker` does also provide another standalone function called `mask()`. It can be imported like that.\n\n```js\nimport { mask } from 'rxjs-broker';\n```\n\nThe `mask()` function takes a JSON object which gets used to extract incoming data and to enhance outgoing data. If there is for example a DataChannel which receives two types of messages (control messages and measurement messages), they might look somehow like this:\n\n```json\n{\n    \"type\": \"control\",\n    \"message\": {\n        \"heating\": \"off\"\n    }\n}\n```\n\n```json\n{\n    \"type\": \"measurement\",\n    \"message\": {\n        \"temperature\": \"30°\"\n    }\n}\n```\n\nIn case you are not interested in the messages of type control and only want to receive and send messages of type measurement, you can use `mask()` to achieve exactly that.\n\n```js\nconst maskedSubject = mask({ type: 'measurement' }, dataChannelSubject);\n\n// The callback will be called with unwrapped messages like { temperature: '30°' }.\nmaskedSubject.subscribe((message) =\u003e {\n    // ...\n});\n```\n\nWhen you call `next()` or `send()` on the returned `IRemoteSubject` it also wraps the message with the provided mask. Considering the example introduced above, the usage of the `send()` method will look like this:\n\n```js\nconst maskedSubject = mask({ type: 'measurement' }, dataChannelSubject);\n\n// This will send wrapped messages like { type: 'measurement', message: { temperature: '30°' } }.\nmaskedSubject.send({ temperature: '30°' });\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisguttandin%2Frxjs-broker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrisguttandin%2Frxjs-broker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisguttandin%2Frxjs-broker/lists"}