{"id":28398392,"url":"https://github.com/primus/substream","last_synced_at":"2025-07-11T15:35:11.011Z","repository":{"id":10979106,"uuid":"13297106","full_name":"primus/substream","owner":"primus","description":"Volatile namespaces on top of Primus streams","archived":false,"fork":false,"pushed_at":"2022-12-31T20:16:45.000Z","size":83,"stargazers_count":33,"open_issues_count":7,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-07-06T02:42:46.832Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/primus.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":"2013-10-03T12:04:38.000Z","updated_at":"2023-09-20T07:24:02.000Z","dependencies_parsed_at":"2023-01-13T16:16:12.324Z","dependency_job_id":null,"html_url":"https://github.com/primus/substream","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/primus/substream","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primus%2Fsubstream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primus%2Fsubstream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primus%2Fsubstream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primus%2Fsubstream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/primus","download_url":"https://codeload.github.com/primus/substream/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primus%2Fsubstream/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264841584,"owners_count":23671900,"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":"2025-06-01T04:38:36.542Z","updated_at":"2025-07-11T15:35:10.985Z","avatar_url":"https://github.com/primus.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SubStream\n\n[![Version npm](https://img.shields.io/npm/v/substream.svg?style=flat-square)](https://www.npmjs.com/package/substream)[![CI](https://img.shields.io/github/actions/workflow/status/primus/substream/ci.yml?branch=master\u0026label=CI\u0026style=flat-square)](https://github.com/primus/substream/actions?query=workflow%3ACI+branch%3Amaster)\n\nSubStream is a simple stream multiplexer for [Primus]. It allows you to create\nsimple message channels which only receive the information you send to it. These\nchannels are in fact, streams, which is why this module is called `SubStreams`\nbecause it adds small streams on top of the main stream and intercepts them.\n\n## Installation\n\n```bash\nnpm install --save substream\n```\n\nThe module can only be used in conjunction with [Primus] so make sure that your\napplication is using that as its real-time backend.\n\n## Getting started\n\nIn all the code examples, we assume that the following code is present:\n\n```js\n'use strict';\n\nvar Primus = require('primus')\n  , http = require('http');\n\nvar server = http.createServer()\n  , primus = new Primus(server);\n\n//\n// Custom code here, just above the listen call.\n//\n\nserver.listen(8080);\n```\n\nWhich is the most minimal bootstrapping code required to create a [Primus]\npowered server. Once you've setup the server you need to add `SubStream` as\na plugin in to [Primus]:\n\n```js\n//\n// The `primus.plugin` method adds the plugin to primus. It requires a unique\n// name in order to easily retrieve it again. For the\n// sake of clarity, we're going to use 'substream' as the name.\n//\nprimus.plugin('substream', require('substream'));\n```\n\nAfter you've added plugins, you might want to re-compile the client library that\n[Primus] serves, as it automatically adds the client-side plugin to the framework,\nas well as the custom `substream.js` library to create the actual name spaces. To\nsave the client just run:\n\n```js\nprimus.save(__dirname +'/primus.js');\n```\n\nBut this is only needed if you serve the file manually and not through the\nautomatically generated `/primus/primus.js` path (which is regenerated on the fly).\nNow that we've set everything up correctly we can start creating some substreams.\n\n### The client\n\nTo create or access a `substream` in the [Primus] client start off with making\na connection:\n\n```js\nvar primus = new Primus('http://\u003cyour url here:whateverportnumber\u003e');\n\nvar foo = primus.substream('foo');\n```\n\nThe `substream` method automatically creates a namespaced stream if you didn't\ncreate it before. Or, it will return your previously created stream when you call\nit again. So now we have a `foo` stream we can just write to:\n\n```js\nfoo.write('data');\n```\n\nAwesome, all works as intended. But this was just one single substream, we can\nadd more:\n\n```js\nvar bar = primus.substream('bar')\n  , baz = primus.substream('baz');\n```\n\nYou can create an infinite number of substreams on top of one single base stream.\nThe data is not leaked between streams. It's all \"sandboxed\".\n\nAs the returned substreams are `streams` or `eventemitters` we can just listen\nto `data`, `end` or `close` events. But it also proxies all the other events\nthat [Primus] emits such as the `reconnect`, `offline` events etc. (The full\nlist is in the [Primus] README.md). So for receiving and writing data you can just\ndo:\n\n```js\nbar.on('data', function () {\n\n});\n\nbar.write('hello from bar');\n\nfoo.on('data', function (data) {\n  console.log('recieved data', data);\n}).on('end', function () {\n  console.log('connection has closed or substream was closed');\n});\n```\n\n### The server\n\nThe server portion of this module isn't that different than the client portion.\nIt follows the same API stream/eventemitter API:\n\n```js\nprimus.on('connection', function (spark) {\n  var foo = spark.substream('foo')\n    , bar = spark.substream('bar')\n    , baz = spark.substream('baz');\n\n  foo.on('data', function (data) {\n    console.log('foo received:', data);\n  });\n\n  //\n  // You can even pipe data\n  //\n  fs.createReadSteam(__dirname + '/example.js').pipe(bar, {\n    end: false\n  });\n\n  //\n  // To stop receiving data, simply end the substream:\n  //\n  baz.end();\n})\n```\n\n### FYI's\n\n- When the spark/connection closes all SubStreams will automatically close, this\n  only happens for the end event, the close event is proxied.\n- We add an extra `streams` object to the spark/connection which contains all\n  active SubStreams for the given connection mapped by name-\u003esubstream-instance\n\n## License\n\n[MIT](LICENSE)\n\n[Primus]: https://github.com/primus/primus\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprimus%2Fsubstream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprimus%2Fsubstream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprimus%2Fsubstream/lists"}