Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
websocket-stream
Advanced tools
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.
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);
The 'ws' package is a popular WebSocket implementation for Node.js. It provides a comprehensive set of features for working with WebSockets, including support for both client and server. Unlike websocket-stream, 'ws' does not integrate directly with the Node.js stream API, but it offers more control over WebSocket connections and is widely used in the community.
The 'socket.io' package is a real-time communication library that provides both WebSocket and fallback support for other transport protocols. It is designed for building real-time applications and offers features like rooms, namespaces, and broadcasting. While it is more feature-rich than websocket-stream, it is also more complex and may be overkill for simple WebSocket streaming use cases.
The 'websocket' package is another WebSocket implementation for Node.js. It supports both client and server and provides a simple API for working with WebSocket connections. It does not integrate with the Node.js stream API like websocket-stream, but it is a solid choice for basic WebSocket functionality.
npm install websocket-stream
use HTML5 websockets the node way -- with streams
you can use browserify to package this module for browser use.
var websocket = require('websocket-stream')
var ws = websocket('ws://realtimecats.com')
ws.pipe(somewhereAwesome)
ws
is a stream and speaks stream events: data
, error
and end
. that means you can pipe output to anything that accepts streams. you can also pipe data into streams (such as a webcam feed or audio data)
npm install -g browserify // install browserify
cd node_modules/websocket-stream
npm install . // install dev dependencies
browserify index.js -s websocket-stream > websocket-stream.js // require websocket-stream.js in your client-side app
using the ws
module you can make a websocket server and use this module to get websocket streams on the server:
var WebSocketServer = require('ws').Server
var websocket = require('websocket-stream')
var wss = new WebSocketServer({server: someHTTPServer})
wss.on('connection', function(ws) {
var stream = websocket(ws)
fs.createReadStream('bigdata.json').pipe(stream)
})
pass in options as the second argument like this:
websocketStream('ws://foobar', { type: someTypedArray })
// e.g. {type: Uint8Array} means you'll get Uint8Arrays back instead of ArrayBuffers
possible options are...
{
protocol: // optional, string, specify websocket protocol
type: // optional, TypedArray object, wraps the ArrayBuffer before emitting
}
To send binary data just write a Buffer or TypedArray to the stream.
On the other end you will receive Buffer instances if it's the server and ArrayBuffer instances if it's the client. The client will default to ArrayBuffer
objects but can also be configured to receive Blob
s.
If you write binary data to a websocket on the server, the client will receive binary objects. Same thing goes for strings.
BSD LICENSE
FAQs
Use websockets with the node streams API. Works in browser and node
The npm package websocket-stream receives a total of 177,138 weekly downloads. As such, websocket-stream popularity was classified as popular.
We found that websocket-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.