Socket
Socket
Sign inDemoInstall

@awesomeorganization/sse-handler

Package Overview
Dependencies
0
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @awesomeorganization/sse-handler

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


Version published
Weekly downloads
32
increased by166.67%
Maintainers
1
Install size
8.76 kB
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 24 Sep 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc