npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sap_oss/node-socketio-stream

v1.0.8

Published

stream module for Socket.IO

Downloads

716

Readme

Stream module for Socket.IO

This library is an update to the socket.io-stream node module. No code changes have been made, but the debug module has been updated to avoid a security vulnerability in the existing project.

REUSE status

Requirements

  • node.js 4.x or higher

Download and installation

npm install @sap_oss/node-socketio-stream

Known issues

Issues on the original socket.io-stream module (listed here) also apply to this module.

How to obtain support

This project is provided as-is. No new features or bug fixes are anticipated.

License

Copyright 2020-2023 SAP SE or an SAP affiliate company and node-socketio-stream contributors. See also the LICENSE file.

Usage

If you are not familiar with Stream API, be sure to check out the docs. I also recommend checking out the awesome Stream Handbook.

For streaming between server and client, you will send stream instances first. To receive streams, you just wrap socket with node-socketio-stream, then listen any events as usual.

Server:

var io = require('socket.io').listen(80);
var ss = require('@sap_oss/node-socketio-stream');
var path = require('path');

io.of('/user').on('connection', function(socket) {
  ss(socket).on('profile-image', function(stream, data) {
    var filename = path.basename(data.name);
    stream.pipe(fs.createWriteStream(filename));
  });
});

createStream() returns a new stream which can be sent by emit().

Client:

var io = require('socket.io-client');
var ss = require('@sap_oss/node-socketio-stream');

var socket = io.connect('http://example.com/user');
var stream = ss.createStream();
var filename = 'profile.jpg';

ss(socket).emit('profile-image', stream, {name: filename});
fs.createReadStream(filename).pipe(stream);

You can stream data from a client to server, and vice versa.

// send data
ss(socket).on('file', function(stream) {
  fs.createReadStream('/path/to/file').pipe(stream);
});

// receive data
ss(socket).emit('file', stream);
stream.pipe(fs.createWriteStream('file.txt'));

Browser

This module can be used on the browser. To do so, just copy a file to a public directory.

$ cp node_modules/@sap_oss/node-socketio-stream/node-socketio-stream.js somewhere/public/

You can also use browserify to create your own bundle.

$ npm install browserify -g
$ cd node_modules/@sap_oss/node-socketio-stream
$ browserify index.js -s ss > node-socketio-stream.js
<input id="file" type="file" />

<script src="/socket.io/socket.io.js"></script>
<script src="/js/node-socketio-stream.js"></script>
<script src="/js/jquery.js"></script>
<script>
$(function() {
  var socket = io.connect('/foo');

  $('#file').change(function(e) {
    var file = e.target.files[0];
    var stream = ss.createStream();

    // upload a file to the server.
    ss(socket).emit('file', stream, {size: file.size});
    ss.createBlobReadStream(file).pipe(stream);
  });
});
</script>

Upload progress

You can track upload progress like the following:

var blobStream = ss.createBlobReadStream(file);
var size = 0;

blobStream.on('data', function(chunk) {
  size += chunk.length;
  console.log(Math.floor(size / file.size * 100) + '%');
  // -> e.g. '42%'
});

blobStream.pipe(stream);

Socket.IO v0.9 support

You have to set forceBase64 option true when using the library with socket.io v0.9.x.

ss.forceBase64 = true;

Documentation

ss(sio)

  • sio socket.io Socket A socket of Socket.IO, both for client and server
  • return Socket

Look up an existing Socket instance based on sio (a socket of Socket.IO), or create one if it doesn't exist.

socket.emit(event, [arg1], [arg2], [...])

  • event String The event name

Emit an event with variable number of arguments including at least a stream.

ss(socket).emit('myevent', stream, {name: 'thefilename'}, function() { ... });

// send some streams at a time.
ss(socket).emit('multiple-streams', stream1, stream2);

// as members of array or object.
ss(socket).emit('flexible', [stream1, { foo: stream2 }]);

// get streams through the ack callback
ss(socket).emit('ack', function(stream1, stream2) { ... });

socket.on(event, listener)

  • event String The event name
  • listener Function The event handler function

Add a listener for event. listener will take stream(s) with any data as arguments.

ss(socket).on('myevent', function(stream, data, callback) { ... });

// access stream options
ss(socket).on('foo', function(stream) {
  if (stream.options && stream.options.highWaterMark > 1024) {
    console.error('Too big highWaterMark.');
    return;
  }
});

ss.createStream([options])

  • options Object
    • highWaterMark Number
    • encoding String
    • decodeStrings Boolean
    • objectMode Boolean
    • allowHalfOpen Boolean if true, then the stream won't automatically close when the other endpoint ends. Default to false.
  • return Duplex Stream

Create a new duplex stream. See the docs for the details of stream and options.

var stream = ss.createStream();

// with options
var stream = ss.createStream({
  highWaterMark: 1024,
  objectMode: true,
  allowHalfOpen: true
});

ss.createBlobReadStream(blob, [options])

  • options Object
    • highWaterMark Number
    • encoding String
    • objectMode Boolean
  • return Readable Stream

Create a new readable stream for Blob and File on browser. See the docs for the details of stream and options.

var stream = ss.createBlobReadStream(new Blob([1, 2, 3]));

ss.Buffer

Node Buffer class to use on browser, which is exposed for convenience. On Node environment, you should just use normal Buffer.

var stream = ss.createStream();
stream.write(new ss.Buffer([0, 1, 2]));