distobj
a basic system for distributed objects in Node.js
Last updated 6 years ago by unixpickle .
GPL · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install distobj 
SYNC missed versions from official npm registry.

Distributed Objects in Node.js

distobj provides a basic facility for accessing remote EventEmitters over the network in Node.js. Internally, distobj uses JSON to encode arguments you pass, and to encode responses (which come in the form of events). Since Node.js is totally event based, distobj has no concept of return values.

Usage

You can create and publish an object as follows in this CoffeeScript example:

{Emitter, Server} = require 'distobj'
net = require 'net'

# simply subclass the Emitter class
class TestObject extends Emitter
  print: (data) ->
    console.log data
    # events we emit are broadcasted
    @emit 'echo', data
  add: (a, b) ->
    @emit 'sum', a + b

# create a new TestObject.
object = new TestObject()
# create a server with object on port 1337.
server = net.createServer()
realObj = new Server server, object
server.listen 1337

To access that object from a different process, do this:

{Client} = require 'distobj'
net = require 'net'
# create a connection
socket = net.connect port: 1337, ->
  # create a client and await a callback
  c = new Client socket, ->
    # upon receiving this callback, the client is now configured
    # with all of the methods on the remote object
    c.add 10, 20
    c.print 'hey there'
  # treat c like a normal event emitter
  c.on 'sum', (n) ->
    console.log 'got sum: ' + n
  c.on 'echo', (d) ->
    console.log 'got echo: ' + d

As you can see, distobj makes it almost too easy to access objects over the network!

Current Tags

  • 0.0.6                                ...           latest (6 years ago)

6 Versions

  • 0.0.6                                ...           6 years ago
  • 0.0.5                                ...           6 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 4
Dependencies (2)
Dev Dependencies (0)
None
Dependents (2)

Copyright 2014 - 2017 © taobao.org |