{"id":28398388,"url":"https://github.com/primus/primus-emit","last_synced_at":"2025-10-20T06:53:05.195Z","repository":{"id":15775184,"uuid":"18514221","full_name":"primus/primus-emit","owner":"primus","description":"Emitting events between server and client.","archived":false,"fork":false,"pushed_at":"2022-12-31T20:04:51.000Z","size":48,"stargazers_count":20,"open_issues_count":2,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-06-28T14:41:34.504Z","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":"2014-04-07T11:02:50.000Z","updated_at":"2025-04-19T12:47:10.000Z","dependencies_parsed_at":"2023-01-11T20:19:34.698Z","dependency_job_id":null,"html_url":"https://github.com/primus/primus-emit","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/primus/primus-emit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primus%2Fprimus-emit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primus%2Fprimus-emit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primus%2Fprimus-emit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primus%2Fprimus-emit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/primus","download_url":"https://codeload.github.com/primus/primus-emit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primus%2Fprimus-emit/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263089475,"owners_count":23412313,"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:34.942Z","updated_at":"2025-10-20T06:53:05.106Z","avatar_url":"https://github.com/primus.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# primus-emit\n\n[![Version npm](https://img.shields.io/npm/v/primus-emit.svg?style=flat-square)](https://www.npmjs.com/package/primus-emit)[![CI](https://img.shields.io/github/actions/workflow/status/primus/primus-emit/ci.yml?branch=master\u0026label=CI\u0026style=flat-square)](https://github.com/primus/primus-emit/actions?query=workflow%3ACI+branch%3Amaster)[![Coverage Status](https://img.shields.io/coveralls/primus/primus-emit/master.svg?style=flat-square)](https://coveralls.io/r/primus/primus-emit?branch=master)\n\nThe `primus-emit` module adds `client-\u003eserver` and `server-\u003eclient` event emitting to\nPrimus.\n\n## Installation\n\nThe module is released under the name `primus-emit`:\n\n```\nnpm install --save primus-emit\n```\n\nThe `--save` flags tells `npm` to automatically add the package and it's\ninstalled version as dependency.\n\n## Adding it to Primus\n\nThe module should be used through Primus's plugin system. In the following\nexamples we assume that your server has been setup as:\n\n```js\n'use strict';\n\nvar Primus = require('primus')\n  , server = require('http').createServer();\n  , primus = new Primus(server, { transformer: 'websockets' });\n```\n\nNow that the server and Primus instance has been created we can add the plugin\nwith the server. Adding plugins is done with the `primus.plugin` method:\n\n```js\nprimus.plugin('emit', require('primus-emit'));\n```\n\nAnd that is everything that you need. The module doesn't require any\nconfiguration.\n\nIf you are manually saving the compiled Primus client to disk make sure you\ninclude the plugins before calling the `primus.save`, `primus.library` or\n`primus.Socket` methods or properties as you will be compiling the client file\n**without** the added plugins.\n\n## Usage\n\nThere are a couple exceptions on the events that you can emit between the server\nand client. We automatically blacklist reserved event names. This ensures that\nyou cannot accidentally emit the `end` event on the client and close the\nconnection on the server etc. The blacklisted events are all events that are\nprefixed with `incoming:` and `outgoing:` as they are used by Primus internally\nand all events client or server emits. See https://github.com/primus/primus#events\nfor an overview of all events that are emitted by Primus.\n\n### Server\n\nTo emit an event from the server to the client you can simply call the `emit`\nmethod:\n\n```js\nprimus.on('connection', function connection(spark) {\n  spark.emit('event-name', 'arguments');\n\n  //\n  // To receive events, simply add a listener for it.\n  //\n  spark.on('custom-event', function custom(data, another, arg) {\n    assert.equal(data.foo, 'foo');\n    assert.equal(another, 1);\n    assert.equal(arg, 'bar');\n\n    this.emit('foo', 'bar');\n  });\n});\n```\n\n### Broadcasting\n\nIf you want to send an event to every connected client on your server you can\nsimply do that by iterating over the connections.\n\n```js\nprimus.forEach(function (spark) {\n  spark.emit('broadcast', 'event');\n});\n```\n\n### Client\n\nSending events on the client is just as simple as on the server.\n\n```js\nvar primus = new Primus('http://localhost:port');\n\n//\n// We can listen to events that are emitted from the server.\n//\nprimus.on('event-name', function (arg) {\n  assert.equal(arg, 'arguments');\n\n  //\n  // Or emit our own events to the server.\n  //\n  this.emit('custom-event', { foo: 'foo' }, 1, 'bar');\n});\n\nprimus.on('foo', function (bar) {\n  assert.equal(bar, 'bar');\n  primus.emit('foo', bar);\n});\n```\n\n## FAQ\n\n### How is different than `primus-emitter`\n\nThere are a couple of differences between this module and the `primus-emitter`\nmodule. The only similarity that they have is that they both emit events. The\nmain differences are:\n\n1. **method name** The `primus-emitter` module adds a special `send` method to\n   the prototypes while we re-use the `emit` method. This makes the code much\n   more portable as it uses the same method name node's EventEmitter.\n2. **Focus** This module only focuses on one thing, emitting events. The\n   `primus-emitter` ships with a lot more features that are not needed for\n   emitting events.\n3. **Small** The footprint of this module is really small. The whole code base is\n   only 80 lines of code including comments. We use the bare minimal code in\n   order to work. This makes maintenance a lot easier.\n\n### Why was this module created\n\nThis module was written as part of the plugin documentation for Primus. Writing\nan EventEmitter was the ideal use case as it:\n\n- Uses the Primus message transformation for message interception.\n- Extends the client and server.\n- Is small enough to understand.\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprimus%2Fprimus-emit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprimus%2Fprimus-emit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprimus%2Fprimus-emit/lists"}