Socket
Socket
Sign inDemoInstall

websocket-stream

Package Overview
Dependencies
Maintainers
3
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

websocket-stream

Use websockets with the node streams API. Works in browser and node


Version published
Weekly downloads
260K
decreased by-16.07%
Maintainers
3
Weekly downloads
 
Created

What is websocket-stream?

The websocket-stream npm package allows you to use WebSockets as a streaming interface in Node.js. It integrates WebSockets with the Node.js stream API, making it easier to work with real-time data streams over WebSocket connections.

What are websocket-stream's main functionalities?

Creating a WebSocket Stream Server

This code sets up an HTTP server and attaches a WebSocket stream server to it. The WebSocket server listens for incoming connections and echoes back any data it receives.

const websocket = require('websocket-stream');
const http = require('http');

const server = http.createServer();
const wss = websocket.createServer({ server }, function(stream) {
  stream.pipe(stream); // Echoes back the received data
});

server.listen(8080, () => {
  console.log('WebSocket server is listening on port 8080');
});

Connecting to a WebSocket Stream Server

This code demonstrates how to connect to a WebSocket stream server and send data to it. It also listens for data from the server and logs it to the console.

const websocket = require('websocket-stream');

const stream = websocket('ws://localhost:8080');

stream.write('Hello, server!');
stream.on('data', function(data) {
  console.log('Received:', data.toString());
});

Using WebSocket Streams with Node.js Streams

This code shows how to pipe a file stream to a WebSocket stream. It reads data from a file and sends it over the WebSocket connection.

const websocket = require('websocket-stream');
const fs = require('fs');

const stream = websocket('ws://localhost:8080');
const fileStream = fs.createReadStream('example.txt');

fileStream.pipe(stream);

Other packages similar to websocket-stream

Keywords

FAQs

Package last updated on 09 Jul 2015

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