mcollina / hyperemitter

Horizontally Scalable EventEmitter powered by a Merkle DAG
ISC License
70 stars 2 forks source link

Handling multiple protobuf schemas over multiple connected HyperEmitters #6

Closed mcdonnelldean closed 9 years ago

mcdonnelldean commented 9 years ago

Given the following code:

var fs = require('fs')
var path = require('path')
var HyperEmitter = require('hyperemitter')

var buildDB = require('memdb')
var dbOne = buildDB('one')
var dbTwo = buildDB('two')

var getSchema = require('./getSchema')
var schemaOne = getSchema('one')
var schemaTwo = getSchema('two')

var emitterOne = new HyperEmitter(dbOne, schemaOne)
var emitterTwo = new HyperEmitter(dbTwo, schemaTwo)

emitterOne.listen(9901, function (err) {
 emitterTwo.connect(9901, '127.0.0.1', function (err) {

    // assume is in schemaOne
    var userAddedMsg = {
      id: 1,
      username: 'user'
    }

    // assume is in schemaTwo
    var userRemovedMsg = {
      id: 1
    }

    emitterOne.on('userRemoved', function (msg) {
      console.log('userRemoved: ', msg)
    })

    emitterTwo.on('userAdded', function (msg) {
      console.log('userAdded: ', msg)
    })

    emitterOne.emit('userAdded', userAddedMsg)
    emitterTwo.emit('userRemoved', userRemovedMsg)
  })
})

What do we think should happen? We also need to consider that some encoders may not need or use a schema and would send the messages anyway.

mcollina commented 9 years ago

How about a defineMessage(name, encoderObj) or defineEvent(name, encoderObj) thing? So we can get rid of the whole schema in new HyperEmitter().

My idea is that, if a message is not known, we should emit an event in hyper.status, which is a stock EventEmitter.

mcdonnelldean commented 9 years ago

Ok so there are a couple of parts to your answer, let me see if I have them right:

Are these right?

mcollina commented 9 years ago

No, I think we should everything we don't understand on the main HyperEmitter, as buffers, which is exactly what this thing is about: moving buffers.

Than encodings will do automatic transforms from/to Buffers, for the specified messages.

Yes, let's add a addEncoding() method that takes care of adding that thing.

Is this all you need?

mcdonnelldean commented 9 years ago

Support added in #15