ssejson
Serialize and parse object streams over SSE/EventSource
Last updated 6 years ago by finnpauls .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ssejson 
SYNC missed versions from official npm registry.

ssejson

Windows Mac/Linux
Windows Build status Build Status

Serializing and parsing Object Streams for server sent events using the EventSource api.

On the server side use ssejson.serialize() to turn the objectstream to sse, on the (browser) client use ssejson.fromEventSource() to parse it back to an objectstream.

The format is pretty simple each object chunk is encoded as JSON as the name suggests and serialized and parsed by the module.

Serialize Example

var http = require('http')
var fs = require('fs')
var ssejson = require('ssejson')
var csv = require('csv-parser')
http.createServer(function (req, res) {
  fs.createReadStream('data.csv')
    .pipe(csv())
    .pipe(ssejson.serialize())
    .pipe(res)
})

The serializer allows an options object to be passed as the first argument. There you can specify an event attribute and it will use the name you specify there instead of sending unnamed messages.

fromEventSource Example

For the Use with the browser EventSource api, but should also work with compliant replacements like the eventsource module.

The second argument is optional and declares the name of the event. Without specifying it defaults to unnamed messages.

var htmltable = require('htmltable')
var ssejson = require('ssejson')

ssejson.fromEventSource(new EventSource('/'), 'data')
  .pipe(htmltable(document.querySelector('#data')))

Parse Example

If you have access to the raw sse you can parse it this way. You can specify the event option to parse named events.

  var request = require('request')
  var ssejson = require('ssejson')
  
  request('/sse')
    .pipe(ssejson.parse())
    .on('data', function (row) {
      console.log(row)
    })

Current Tags

  • 1.2.0                                ...           latest (6 years ago)

5 Versions

  • 1.2.0                                ...           6 years ago
  • 1.1.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 3
This Month 4
Last Day 2
Last Week 1
Last Month 17
Dependencies (3)
Dev Dependencies (3)

Copyright 2014 - 2016 © taobao.org |