New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@alshdavid/chi

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alshdavid/chi

Lightweight utilities for use with the Node standard http library

  • 0.2.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

chi

Lightweight utilities for use with the Node standard http library

ServerEventSource

Bidirectional communication using server sent events

Server

import * as http from 'node:http'
import { ServerSentEvents } from '@alshdavid/chi'

const hostname = '0.0.0.0';
const port = 3000;

const sse = new ServerSentEvents()

// Run when a new client connects
sse.on_connect(({ id }) => {
  console.log('Connected', id)

  // Broadcast to all connected clients
  sse.send_all({ message: 'New client joined!', id })

  // When a specific client sends message
  sse.on_message(id, (msg) => {
    console.log('from client', id, msg)
  })

  // Send a message to a specific client
  setTimeout(async () => {
    while (true) {
      sse.send(id, { message: 'Hello!' })
      await new Promise(res => setTimeout(res, 1000))
    }
  })
})

// Listen for a message from all clients
sse.on_message_all((id, msg) => {
  console.log('from client (all)', id, msg)
})

// Run when a new client disconnects
sse.on_disconnect(({ id }) => {
  console.log('Disconnected', id)
})

// Set up a standard HTTP server
const server = http.createServer(async (req, res) => {
  // Add route for server-to-client
  if (req.url === '/sse' && req.method === 'GET') {
    await sse.http_handler_on_connection(req, res)
    return
  }

  // Add route for client-to-server
  if (req.url === '/sse' && req.method === 'POST') {
    await sse.http_handler_on_message(req, res)
    return
  }

  res.statusCode = 404
  res.end()
})

server.listen(port, hostname, () => {
  console.log(`Server running on http://${hostname}:${port}/`);
});

Client

import { SSEClient } from '@alshdavid/chi'

const sse = new SSEClient({ 
  endpoint: '/sse',
})

sse.on(msg => console.log(msg))

sse.send({ message: 'hello world' })

FAQs

Package last updated on 17 Jan 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc