Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

websocket-iterator

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

websocket-iterator

turns WebSocket into async iterator

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1K
decreased by-31.93%
Maintainers
1
Weekly downloads
 
Created
Source

websocket-iterator

Tiny adapter to iterate over websocket. Correctly closes websocket on loop break.

websocketData

async function* websocketData(socket: WebSocket)

Allows to iteratate over data emited by websocket.

websocketEvents

async function* websocketEvents(socket: WebSocket, options?: { emitOpen: boolean })

Allows to iteratate over message events emited by websocket. If emitOpen is true then also yields open event.

code:

import { websocketData, websocketEvents } from 'websocket-iterator'

async function () {
  const socket = new WebSocket('ws://server/path/to/socket')
  for await (const data of websocketData(socket)) {
  	// ...
  }

  // emit events
  const socket = new WebSocket('ws://server/path/to/socket')
  for await (const { data } of websocketEvents(socket)) {
  	// ...
  	if (data === undefined) // socket is just opened
	    socket.send(something1)
	else
	    socket.send(something2)
	// ...
  }
}

Keywords

FAQs

Package last updated on 07 May 2020

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