{"id":16834689,"url":"https://github.com/mscdex/bellhop","last_synced_at":"2025-08-03T06:15:26.089Z","repository":{"id":11540219,"uuid":"14025148","full_name":"mscdex/bellhop","owner":"mscdex","description":"A node.js module that exposes streams for doing Pubsub and RPC.","archived":false,"fork":false,"pushed_at":"2014-01-16T15:50:38.000Z","size":333,"stargazers_count":28,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-10T18:53:04.615Z","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/mscdex.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-31T18:31:33.000Z","updated_at":"2023-05-14T04:05:47.000Z","dependencies_parsed_at":"2022-08-31T06:40:07.019Z","dependency_job_id":null,"html_url":"https://github.com/mscdex/bellhop","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mscdex%2Fbellhop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mscdex%2Fbellhop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mscdex%2Fbellhop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mscdex%2Fbellhop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mscdex","download_url":"https://codeload.github.com/mscdex/bellhop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244799327,"owners_count":20512235,"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:07:24.073Z","updated_at":"2025-03-22T04:30:45.964Z","avatar_url":"https://github.com/mscdex.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nDescription\n===========\n\nA node.js module that exposes streams for doing Pubsub and RPC.\n\n\nRequirements\n============\n\n* [node.js](http://nodejs.org/) -- v0.10.0 or newer\n\n\nInstall\n============\n\n    npm install bellhop\n\n\nExamples\n========\n\n* Simple RPC (no intermediate medium/stream):\n\n```javascript\nvar RPC = require('bellhop').RPC;\n\nvar RPC_Server = new RPC(),\n    RPC_Client = new RPC();\n\n// Server\n\nRPC_Server.add({\n  add: function(a, b, cb) {\n    cb(a + b);\n  },\n  serverDate: function(cb) {\n    cb(new Date());\n  },\n  log: function(fn, cb) {\n    cb(fn());\n  }\n});\n\n// Client\n\nRPC_Client.send(5, 5, 'add', function(err, result) {\n  console.log('add() result = ' + result);\n});\nRPC_Client.send('serverDate', function(err, date) {\n  console.log('serverDate() date UNIX timestamp: ' + date.getTime());\n});\nRPC_Client.send(function() {\n  console.log('Look at me, I am running on the server!');\n}, 'log', function(err) {\n  console.log('log() Finished executing function on server');\n});\n\n// simple in-process RPC\nRPC_Client.pipe(RPC_Server).pipe(RPC_Client);\n\n\n// Example output:\n//\n// Look at me, I am running on the server!\n// add() result = 10\n// serverDate() date UNIX timestamp: 1384958692554\n// log() Finished executing function on server\n```\n\n* RPC over HTTP:\n\n```javascript\nvar http = require('http');\nvar RPC = require('bellhop').RPC;\n\nfunction multiply(a, b, cb) {\n  cb(a * b);\n}\n\nvar TOTAL = 10, count = 0;\n\nhttp.createServer(function(req, res) {\n  var RPC_Server = new RPC();\n  RPC_Server.add(multiply);\n  req.pipe(RPC_Server).pipe(res);\n  if (++count === TOTAL)\n    this.close();\n}).listen(8080, function() {\n  console.log('HTTP RPC server listening');\n\n  for (var i = 0; i \u003c TOTAL; ++i) {\n    (function(n) {\n      var RPC_Client = new RPC(),\n          req = http.request({\n            host: '127.0.0.1',\n            port: 8080,\n            method: 'POST',\n            agent: false\n          }, function(res) {\n            res.pipe(RPC_Client);\n          });\n      RPC_Client.pipe(req);\n      RPC_Client.send(2, n, 'multiply', function(err, result) {\n        console.log('Result = ' + result);\n        req.socket.end();\n      });\n    })(i);\n  }\n});\n\n// Output:\n//\n// HTTP RPC server listening\n// Result = 0\n// Result = 2\n// Result = 4\n// Result = 6\n// Result = 8\n// Result = 10\n// Result = 12\n// Result = 14\n// Result = 16\n// Result = 18\n```\n\n* RPC over HTTP (reusing the same RPC server stream with [Conveyor](https://github.com/mscdex/conveyor)):\n\n```javascript\nvar http = require('http');\nvar Conveyor = require('conveyor'),\n    RPC = require('bellhop').RPC;\n\nvar RPC_Server = new RPC(),\n    c = new Conveyor(RPC_Server);\n\nRPC_Server.add(function multiply(a, b) {\n  var cb = arguments[arguments.length - 1];\n  cb \u0026\u0026 cb(a * b);\n});\n\nvar TOTAL = 10, count = 0;\n\nhttp.createServer(function(req, res) {\n  if (++count === TOTAL)\n    this.close();\n  c.push(req, res);\n}).listen(8080, function() {\n  console.log('HTTP RPC server listening');\n\n  for (var i = 0; i \u003c TOTAL; ++i) {\n    (function(n) {\n      var RPC_Client = new RPC(),\n          req = http.request({\n            host: '127.0.0.1',\n            port: 8080,\n            method: 'POST'\n          }, function(res) {\n            res.pipe(RPC_Client);\n          });\n      RPC_Client.pipe(req);\n      RPC_Client.send(2, n, 'multiply', function(err, result) {\n        console.log('Result = ' + result);\n        req.socket.end();\n      });\n    })(i);\n  }\n});\n\n// Output:\n//\n// HTTP RPC server listening\n// Result = 0\n// Result = 2\n// Result = 4\n// Result = 6\n// Result = 8\n// Result = 10\n// Result = 12\n// Result = 14\n// Result = 16\n// Result = 18\n```\n\n* Simple Pubsub (no intermediate medium/stream):\n\n```javascript\nvar Pubsub = require('bellhop').Pubsub;\n\nvar Pubsub1 = new Pubsub(),\n    Pubsub2 = new Pubsub();\n\nPubsub1.events.on('today', function(date) {\n  console.log('Today is: ' + date);\n});\n\nPubsub2.events.emit('today', new Date());\n\n// simple in-process Pubsub\nPubsub1.pipe(Pubsub2).pipe(Pubsub1);\n\n// Example output:\n//\n// Today is: Thu Oct 31 2013 14:02:00 GMT-0400 (Eastern Daylight Time)\n```\n\n* Simple Pubsub over TCP:\n\n```javascript\nvar net = require('net');\n\nvar Pubsub = require('bellhop').Pubsub;\n\nvar PubsubRecvr = new Pubsub(),\n    PubsubSender = new Pubsub();\n\nnet.createServer(function(sock) {\n  this.close();\n  PubsubSender.pipe(sock);\n  PubsubSender.events.emit('today', new Date());\n}).listen(9000, '127.0.0.1');\n\nvar sock = new net.Socket();\nsock.pipe(PubsubRecvr);\nPubsubRecvr.events.on('today', function(date) {\n  console.log('Today is: ' + date);\n  sock.end();\n});\nsock.connect(9000, '127.0.0.1');\n\n// Example output:\n//\n// Today is: Thu Nov 14 2013 07:52:01 GMT-0500 (Eastern Standard Time)\n```\n\n* RPC AND Pubsub over the same TCP socket:\n\n```javascript\nvar net = require('net');\n\nvar Pubsub = require('bellhop').Pubsub,\n    RPC = require('bellhop').RPC;\n\nvar rpcServer = new RPC(),\n    rpcClient = new RPC(),\n    pub = new Pubsub(),\n    sub = new Pubsub();\n\n// Servers\n\nrpcServer.add(function foo() {\n  console.log('Got RPC');\n});\n\nsub.events.on('bar', function() {\n  console.log('Got event');\n});\n\nnet.createServer(function(sock) {\n  this.close();\n  sock.pipe(rpcServer);\n  sock.pipe(sub);\n}).listen(9000, '127.0.0.1');\n\n// Clients\n\nvar sock = new net.Socket();\nrpcClient.pipe(sock);\npub.pipe(sock);\n\nsock.connect(9000, '127.0.0.1', function() {\n  rpcClient.send('foo');\n  pub.events.emit('bar');\n  sock.end();\n});\n\n// Output:\n//\n// Got RPC\n// Got event\n```\n\n\nBenchmarks\n==========\n\nBenchmarks code can be found in `bench/`.\n\nPubsub benchmark results (as of 11/14/2013) for Core i7-3770k, Windows 7 x64, node v0.10.22:\n\n```\n50000 events (no args): 327ms\n50000 events (3 short strings): 389ms\n50000 events (3 longer strings): 433ms\n50000 events (3 regexps): 654ms\n50000 events (3 functions): 874ms\n50000 events (5 numeric values): 470ms\n```\n\n\nAPI\n===\n\nAll types are _Duplex_ streams.\n\nRPC methods\n-----------\n\n* **(constructor)**([\u003c _object_ \u003eoptions]) - Creates and returns a new RPC instance with the following valid `options`:\n\n    * **serialize** - _boolean_ - Automatically serialize objects that JSON does not support (well)? (Default: true).\n\n    * **ignoreInvalidCall** - _boolean_ - Do not send error responses to incoming function call requests for invalid methods (Default: false).\n\n    * **highWaterMark** - _integer_ - High water mark to use for this stream (Default: Duplex stream default).\n\n* **generate**(\u003c _string_ \u003eremoteFuncName) - _function_ - Returns a function that can be used when calling a particular remote function. This makes things easier than using send() manually. The return value of the returned function is similar to that of Writable.write() and indicates if the high water mark has been reached.\n\n* **send**([\u003c _mixed_ \u003earg1, ..., \u003c _mixed_ \u003eargn, ]\u003c _string_ \u003eremoteFuncName[, \u003c _function_ \u003ecallback]) - _(boolean)_ - Calls the function identified by `remoteFuncName` (with optional arguments). The return value is similar to that of Writable.write() and indicates if the high water mark has been reached.\n\n* **add**(\u003c _function_ \u003emethod[, \u003c _string_ \u003emethodName]) - _(void)_ - Adds a function that can be called by others. `methodName` is optional if `method` is a named function, however you can always override the name with `methodName`. The last argument passed to the function is the callback to call in case the other side requested a response. The return value of the callback is similar to that of Writable.write() and indicates if the high water mark has been reached.\n\n* **remove**([\u003c _function_ \u003emethod][, \u003c _string_ \u003emethodName]) - _(void)_ - Removes a function previously added via add(). The removal process first checks `methodName`, then `method` for a name, and if those two are not set then *all* instances of that function are removed.\n\n\nPubsub properties\n-----------------\n\n* **events** - _EventEmitter_ - This is the event emitter object used to emit events to others and to receive events from others.\n\nPubsub (special) events\n-----------------------\n\n* __*__(\u003c _mixed_ \u003eevent[, \u003c _mixed_ \u003earg1, ..., \u003c _mixed_ \u003eargn]) - Emitted for every event for use as a \"catch-all.\"\n\nPubsub methods\n--------------\n\n* **(constructor)**([\u003c _object_ \u003eoptions]) - Creates and returns a new Pubsub instance with the following valid `options`:\n\n    * **serialize** - _boolean_ - Manually serialize objects that JSON does not support (well)? (Default: true).\n\n    * **highWaterMark** - _integer_ - High water mark to use for this stream (Default: Duplex stream default).\n\n\nNotes\n=====\n\n* Serialization of types unsupported by JSON is an option and is enabled by default.\n\n* Properties of \"non-plain objects\" (e.g. Dates, Functions, Errors, RegExps) are not checked for needed serialization.\n\n* Circular references are not detected.\n\n* RPC-specific:\n\n   * Callback functions follow the error-first argument pattern. Any arguments after the error argument are values returned by the other side.\n\n   * All functions returned by `generate()` will assume that a function passed as the last argument will be a callback to be executed when the server responds. If you need to pass a function to the other side, make sure a non-function value separates it and the optional callback **OR** you can use `send()` directly instead. Examples:\n\n      ```javascript\n      // ....\n      var map = RPC_Client.generate('map');\n\n      // no callback/response (THIS IS WRONG, the mapper function will not be sent to the other side and will be used as a callback instead)\n      map([0, 1, 2, 3, 4], function(n) { return n * 2; });\n\n      // no callback/response\n      map(function(n) { return n * 2; }, [0, 1, 2, 3, 4]);\n\n      // callback/response requested\n      map(function(n) { return n * 2; }, [0, 1, 2, 3, 4], function(result) { console.log('Result: ' + result); });\n\n      // OR always use `send()` directly if you want/need the mapper function to always be the second argument:\n\n      // no callback/response\n      RPC_Client.send([0, 1, 2, 3, 4], function(n) { return n * 2; }, 'map');\n\n      // callback/response requested\n      RPC_Client.send([0, 1, 2, 3, 4], function(n) { return n * 2; }, 'map', function(result) {\n        console.log('Result: ' + result);\n      });\n      ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmscdex%2Fbellhop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmscdex%2Fbellhop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmscdex%2Fbellhop/lists"}