ssejson

1.2.0 • Public • Published

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)
    })

Readme

Keywords

Package Sidebar

Install

npm i ssejson

Weekly Downloads

160

Version

1.2.0

License

MIT

Last publish

Collaborators

  • finnpauls