@awesomeorganization/sse-handler

[ESM] The server-sent events (sse) handler for Node.js according to w3c and whatwg

Usage no npm install needed!

<script type="module">
  import awesomeorganizationSseHandler from 'https://cdn.skypack.dev/@awesomeorganization/sse-handler';
</script>

README

sse-handler

:boom: [ESM] The server-sent events (sse) handler for Node.js according to w3c and whatwg


npm npm npm npm npm npm


Example

Full example in /example folder.

import { http } from '@awesomeorganization/servers'
import { rewriteHandler } from '@awesomeorganization/rewrite-handler'
import { sseHandler } from '@awesomeorganization/sse-handler'
import { staticHandler } from '@awesomeorganization/static-handler'

const example = async () => {
  const rewriteMiddleware = rewriteHandler({
    rules: [
      {
        pattern: '(.*)/

,
        replacement: '$1/index.html',
      },
    ],
  })
  const sseMidleware = sseHandler()
  const staticMiddleware = await staticHandler({
    directoryPath: './static',
  })
  http({
    listenOptions: {
      host: '127.0.0.1',
      port: 3000,
    },
    onListening() {
      setInterval(() => {
        const timestamp = new Date().toISOString()
        sseMidleware.push({
          data: `${timestamp}: Hi!`,
        })
        sseMidleware.push({
          data: [timestamp, 'This is multiline', 'string with event.'].join('\n'),
          event: 'someEvent',
        })
      }, 3e3)
    },
    onRequest(request, response) {
      switch (request.method) {
        case 'GET': {
          switch (request.url) {
            case '/sse': {
              sseMidleware.handle({
                request,
                response,
              })
              return
            }
            default: {
              rewriteMiddleware.handle({
                request,
                response,
              })
              staticMiddleware.handle({
                request,
                response,
              })
              return
            }
          }
        }
      }
      response.end()
    },
  })
  // TRY
  // http://127.0.0.1:3000/
}

example()