http server push with application/octet-stream

15/10/2019

You create a normal express server.
one route to display the html
one route to stream data from the server. (push on custom event)

exemple to push event from the server

const express = require('express')
const app = express()

app.get('/', function (req, res) {
    res.sendFile(__dirname+'/index.html');
})

app.get('/stream', function (req, res) {
    res.writeHead(200, {
        'Content-Type': 'application/octet-stream'
    });
    var i = setInterval(() => res.write('salut'), 1000);
    setTimeout(() => {
        clearInterval(i);
        res.end();
    }, 5000)
})

app.listen(3000, function () {
    console.log('Example app listening on port 3000!')
})

exemple to read events from the browser



  
    Exemple
  
  
    

Open console to see data

Raccourcis