metastream

Attach metadata (JSON) to streams

Usage no npm install needed!

<script type="module">
  import metastream from 'https://cdn.skypack.dev/metastream';
</script>

README

metastream

Allows you to attach metadata (an object that can be JSONified) to streams.

This is an alternative to, for example, just encoding binary data with base64 and sending everything as JSON. base64 uses about 33% more bandwidth so this will avoid that.

Here are a few usage examples in one of my current projects:

  • Transmitting files for backup, currently using protocol buffers to send filename etc. along with data, metastreams would also work fine.

  • Relay/proxy system for a home server behind a router doing NAT, single connection multiplexing multiple connects, sending socket ID along with data.

Note that the data you write must fit in memory.

net = require('net');
Metastream = require('metastream');

var server = net.createServer(function(c) {
  var outp = new Metastream(c);
  outp.write({id: 'bob'}, new Buffer('Bob Test'));
  outp.on(function(meta, data) {
    console.log('server recvd from ' + meta.id + ': ' + data.toString());
  });
});

server.listen(6345);

var client = net.connect({port: 6345}, function() {
  var conn = new Metastream(client);
  conn.on(function(meta, data) {
    console.log('client recvd from ' + meta.id + ': ' + data.toString();
    conn.write({id: 'tom'}, new Buffer("Tom received data " + data.toString() + ' back at yah'));
  });
});