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

socket.io-stream

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

socket.io-stream

stream for socket.io

  • 0.5.1
  • Source
  • npm
  • Socket score

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

Socket.IO stream

Build Status NPM version

This is the module for bidirectional binary data transfer with Stream 2 API through Socket.IO.

Installation

$ npm install socket.io-stream

Usage

For streaming between servers and clients, you must send stream instances first. To receive streams, you just wrap socket with socket.io-stream, then listen any events as usual.

Server:

var io = require('socket.io').listen(80);
var ss = require('socket.io-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() will return a new stream which can be sent by emit.

Client:

var io = require('socket.io-client');
var ss = require('socket.io-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.

Browser

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

$ cp node_modules/socket.io-stream/socket.io-stream.js somewhere/public/

You can also use browserify to build manually.

$ npm install browserify -g
$ cd node_modules/socket.io-stream
$ browserify index.js -s ss > socket.io-stream.js
<input id="file" type="file" />

<script src="/socket.io/socket.io.js"></script>
<script src="/js/socket.io-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>

Documentation

ss(sio)

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, ...)

Emit an event with variable args including at least a stream.

socket.on(event, [options], [listener])

Add a listener for event. listener will take streams with any data as arguments. options is an object for streams.

ss.createStream([options])

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

ss.createBlobReadStream(blob, [options])

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

License

MIT

Keywords

FAQs

Package last updated on 09 Dec 2013

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