Git Product home page Git Product logo

level-live-stream's Introduction

level-live-stream

Stream live changes from levelup.

Build Status

Works just like LevelUp#readStream except instead of ending, it will stay open and stream changes to the database as they are inserted!

Just like the couchdb changes feed, but for any arbitrary range of keys!

see also, level-livefeed for the same idea, but with a streams2 api.

var level = require('level')

level('/tmp/level-live-stream', 
  {createIfMissing: true}, function (err, db) {

  var liveStream = require('level-live-stream')(db)

  liveStream
    .on('data', console.log)

  setInterval(function () {
    db.put('time', new Date().toString())

  }, 1000)

})

You can also attach liveStream as a method to the db like below. This will allow multilevel clients to use db.liveStream().

var LiveStream = require('level-live-stream');

// attach the `liveStream` method using `install`
LiveStream.install(db);

// then invoke the method using the `db` instance
db.liveStream()
  .on('data', console.dir);

options

LiveStream(db, {
  tail: true,   //follow
  old : true,   //get old records from data base
                //if old=false you will only get live updates
  min : loKey,  //lowest key in range
  max : hiKey,  //highest key in range
  reverse: true //stream in reverse (only applies to old records)
})

License

MIT

level-live-stream's People

Contributors

dominictarr avatar evbogue avatar juliangruber avatar maiah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

level-live-stream's Issues

multiple data events on single insert when live-streaming a sublevel

level-live-stream v1.4.9
level-sublevel v 5.1.1

I'm creating a livestream on a sublevel, and when I do an insert on that sublevel I get multiple data events. Heres some code to duplicate:

var MemDB = require('memdb');
var sublevel = require('level-sublevel')
var LiveStream = require('level-live-stream')

var db = sublevel(MemDB({ valueEncoding: 'json' }));
LiveStream.install(db)
db.posts = db.sublevel('posts')
LiveStream.install(db.posts)
var post = {
  title: 'a title ',
  body: 'lorem ipsum',
  author: 'julian'
};
db.posts.createLiveStream().on('data',console.log)
db.posts.put('1337',post)

heres the output:

{ key: '1337',
  value: { title: 'a title ', body: 'lorem ipsum', author: 'julian' } }
{ key: '1337',
  value: { title: 'a title ', body: 'lorem ipsum', author: 'julian' },
  type: 'put' }

If i listen to the livestream for the root 'db' i get a single event, which makes sense:

{ type: 'put',
  key: 'ÿpostsÿ1337',
  value: { title: 'a title ', body: 'lorem ipsum', author: 'julian' } }

live-streams throw when closed

There's a call to removeHook() which apparently is not a function. Had a look into level-hooks and can't see any way currently to remove hooks, so this is most likely the problem.

stack overflow when installing live-stream on a root db which has sublevels

Let's say on the server you have something like:

var db = sublevel(db);
var foosub = db.sublevel('foo');

// incorrect
liveStream.install(db);

// correct would be
// liveStream.install(foo);

Consuming on the client side like this:

// incorrect
db.liveStream().on('data', console.dir);

// correct would be
// db.sublevel('foo').liveStream().on('data', console.dir);

Since we did this incorrectly (i.e. attached live stream to root level instead of to the sublevels separately) we'll get a stack overflow error in 'json-buffer' due to the _parent on the sublevels which causes a circular reference:

...../node_modules/multilevel/node_modules/mux-demux/node_modules/json-buffer/index.js:34
    return JSON.stringify(/^:/.test(o) ? ':' + o : o)
                               ^
RangeError: Maximum call stack size exceeded

The solution here is to document to not do that and/or detect this earlier and throw a more meaningful error.

emit('sync') event lost over multilevel

hi, thanks for level-live-stream! :)

i'm using the 'sync' event to know when all the historical data has been read in order to emit that data in a chunk before emitting new chunks based on real-time changes.

assuming the 'sync' event is lost because anything that isn't part of the readable stream is lost, can there be a 'notifyOnSync' option that includes a single piece of data in between reading historical data and reading real-time data like

{
  type: 'sync'
}

?

i implemented the above option here. are these changes worthy of a pull request?

db.post undefined

I guess this needs sublevel. How should I use this with level-stream?

Doing db = Sublevel(db) internally might overwrite stuff.

publish -f

the version of this on npm right now has incorrect dependencies and doesn't match the version in git

Error: not supported

No matter what I do I seem to always receive the following error when using multilevel:

/Users/drewp/Dropbox/js/dataminer/node_modules/multilevel/lib/client.js:107
  if (allowed.indexOf(ev) == -1) throw new Error('not supported');
                                 ^

Error: not supported
    at Db.addEventListener.Db.on (/Users/drewp/Dropbox/js/dataminer/node_modules/multilevel/lib/client.js:107:40)
    at post (/Users/drewp/Dropbox/js/dataminer/node_modules/level-post/index.js:56:6)
    at exports.live (/Users/drewp/Dropbox/js/dataminer/node_modules/pull-level/index.js:22:17)
    at Object.exports.read.exports.readStream.exports.createReadStream (/Users/drewp/Dropbox/js/dataminer/node_modules/pull-level/index.js:50:37)
    at module.exports (/Users/drewp/Dropbox/js/dataminer/node_modules/level-live-stream/index.js:14:35)
    at Db.db.liveStream.db.createLiveStream (/Users/drewp/Dropbox/js/dataminer/node_modules/level-live-stream/index.js:25:14)
    at Object.<anonymous> (/Users/drewp/Dropbox/js/dataminer/examples/sw-saber-miner.js:21:4)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)

Relevant code:

// digger.js
var path = require('path');
var net = require('net');
var level = require('level');
var multilevel = require('multilevel/msgpack');
var LevelLiveStream = require('level-live-stream');

var dbpath = path.join(__dirname, path.basename(__filename, '.js') + '.db');
var db = level(dbpath, {
  createIfMissing: true,
  valueEncoding: 'json'
});

LevelLiveStream.install(db);

net.createServer(con => {
  con
    .pipe(multilevel.server(db))
    .pipe(con);
}).listen(3000);

multilevel.writeManifest(db, __dirname + '/manifest.json');

// ...code that puts a bunch
db.put( 'twitter:' + tweet.id_str, { tweet: tweet, sentiment: 0 });
// miner.js
var path = require('path');
var net = require('net');
var multilevel = require('multilevel/msgpack');
var LevelLiveStream = require('level-live-stream');

var manifest = require('./manifest.json');

var db = multilevel.client(manifest);
var con = net.connect(3000);

LevelLiveStream.install(db);

con
  .pipe(db.createRpcStream())
  .pipe(con);

db.createLiveStream().on('data', console.log.bind(console));

And I've verified that my manifest.json loads and does have createLiveStream/liveStream added to it.

Also tried without the manifest and still got the same error.

Relevant packages:

$ npm ls | grep level
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │   ├── [email protected]
│ │   ├── [email protected]
│ │   ├── [email protected]
│ │   ├── [email protected]
│ └─┬ [email protected]
│   ├── [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ ├─┬ [email protected]
│ │ │ ├─┬ [email protected]
│ │ │ │ └─┬ [email protected]
│ ├─┬ [email protected]
│ │ ├─┬ [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]

Please let me know if there are any more details I can provide!

keys: false

does not work in the live portion of the stream.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.