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

socket.io-file-client

v2.0.2

Published

Client module for Socket.io-file

Downloads

3,434

Readme

Socket.io-file-client

Socket.io-file-client is module for uploading file via Socket.io.

Major Changes from 1.x to 2.x

Socket.io-file 1.x used Binary String to send files. Binary String is little bit slower than direct Binary writes, and also server used fs.write, not writable stream. Recently, FileReader.readAsBinaryString() was deprecated, so I updated Socket.io-file to use ArrayBuffer(Object for manipulate Binary Data directly from JavaScript) instead of Binary String.

Also, newer version has much more functionalities, like Server-side MIME type checking, File size limitations. Even you can configure the size of each transmission(chunk) any value you want, higher value gives you faster upload.

Features

  • Simple is the best.
  • File uploads
  • Highly improved performance
  • Using File Streams to write faster, efficient.
  • Checking mime, limit file size
  • Multiple file uploads

Examples

You can found full source code here: Example Page Or Browserify Example

Client side

<html>
<head>
	<meta charset="UTF-8">
	<title>Socket.io-file 2.x File Upload Example</title>
</head>
<body>
	<h1>Socket.io-file 2.x File Upload Example</h1>
	<p>Select file and click upload button to upload.</p>
	<p>Multiple upload also supports.</p>

	<form id="form">
		<input type="file" id="file" multiple />
		<input type="submit" value="Upload" />
	</form>

	<script src="socket.io.js"></script>
	<script src="socket.io-file-client.js"></script>
	<script src="app.js"></script>
</body>
</html>
var socket = io('http://localhost:3000');
var uploader = new SocketIOFileClient(socket);
var form = document.getElementById('form');

uploader.on('start', function(fileInfo) {
	console.log('Start uploading', fileInfo);
});
uploader.on('stream', function(fileInfo) {
	console.log('Streaming... sent ' + fileInfo.sent + ' bytes.');
});
uploader.on('complete', function(fileInfo) {
	console.log('Upload Complete', fileInfo);
});
uploader.on('error', function(err) {
	console.log('Error!', err);
});
uploader.on('abort', function(fileInfo) {
	console.log('Aborted: ', fileInfo);
});

form.onsubmit = function(ev) {
	ev.preventDefault();
	
	var fileEl = document.getElementById('file');
	var uploadIds = uploader.upload(fileEl, {
		data: { /* Arbitrary data... */ }
	});
};

Also Socket.io-file-client supports UMD, you can load from CommonJS require() or ES6 import:

import SocketIO from 'socket.io-client';
import SocketIOFileClient from 'socket.io-file-client';

var socket = SocketIO('http://localhost:3000');
var uploader = new SocketIOFileClient(socket);
var form = document.getElementById('form');

uploader.on('start', (fileInfo) => {
	console.log('Start uploading', fileInfo);
});
uploader.on('stream', (fileInfo) => {
	console.log('Streaming... sent ' + fileInfo.sent + ' bytes.');
});
uploader.on('complete', (fileInfo) => {
	console.log('Upload Complete', fileInfo);
});
uploader.on('error', (err) => {
	console.log('Error!', err);
});
uploader.on('abort', (fileInfo) => {
	console.log('Aborted: ', fileInfo);
});

form.onsubmit = function(ev) {
	ev.preventDefault();
	
	var fileEl = document.getElementById('file');
	var uploadIds = uploader.upload(fileEl);
};

API

constructor SocketIOFileClient(io socket, Object options)

Create new SocketIOFileClient object.

Array SocketIOFileClient.upload(HTMLElement fileEl, Object options)

Array SocketIOFileClient.upload(FileList files, Object options) (New from 2.0.1)

Upload file(s). First argument must be or FileList object, other wise refuse uploads. If it has multiple files(with multiple attribute), it uploads all at once. Returns array that contains upload ids as values. Available options are:

  • String to: If server has multiple upload directories, client must be set the directory where to upload.
  • Object data: An arbitrary data object that will be passed to the server side events.

SocketIOFileClient SocketIOFileClient.on(String evName, function fn)

Attach event handler to SocketIOFileClient. Possible events are described later part.

SocketIOFileClient SocketIOFileClient.off(String evName[, function fn])

Detach event handler from SocketIOFileClient. If function is undefined, removes all event handlers attached on that event.

SocketIOFileClient SocketIOFileClient.emit(String evName, Object args)

Trigger the event.

void SocketIOFileClient.abort(String id)

Abort upload of specified id.

void SocketIOFileClient.destroy(void)

Destroy all resources about SocketIOFileClient. Use this method for saving more resources from client side. After use this, you can't upload file anymore.

void SocketIOFileClient.getUploadInfo(void)

Get array of currently uploading files. Keys are upload id, values are object that contains information of uploading files.

bool SocketIOFileClient.isDestroyed (ADDED 2.0.2)

Returns true after destroyed by calling destroy method or server force to close.

Events

SocketIOFile provides these events. Some of property is slight different than servers, like wrote -> sent.

ready (ADDED ON 2.0.12)

Fired on ready to upload files. Everytime you create new SocketIOFileClient object, it receives some upload information from server like chunkSize, mimeType like other things. So if you send the file before sync those meta data, your upload won't work properly. Personally, I recommend create single SocketIOFileClient object first, and make upload after ready events fired.

disconnected (ADDED ON 2.0.2)

Fired when server forcely send disconnect order. After it fires, you can't send file via socket.io-file.

loadstart (ADDED ON 2.0.13)

Fired right before load the file from browser. After browser load the file you selected, it will start uploading and trigger "start" event.

progress (ADDED ON 2.0.13)

Keep firing while browser load your file. This will help to implement loading system that how much data loaded before send.

  • Number loaded: Bytes of loaded
  • Number total: Total byts of file

start

Fired on starting file upload. This means server grant your uploading request and create empty file to begin writes. Argument has:

  • String name: Name of the file
  • Number size: Size of the file(bytes)
  • String uploadTo: Directory that where to writing.
  • Object data: The arbitrary data object that was passed to the upload()-function.

stream

Fired on getting chunks from client. Argument has:

  • String name
  • String uploadTo
  • Number size
  • Number sent: Bytes of sent
  • Object data: The arbitrary data object that was passed to the upload()-function.

complete

Fired on upload complete. Argument has:

  • String name
  • String mime: MIME type that server recognized.
  • Number size
  • Number wrote
  • Number estimated: Estimated uploading time as ms.
  • Object data: The arbitrary data object that was passed to the upload()-function.

abort

Fired on abort uploading.

  • String name
  • String uploadTo
  • Number size
  • Number sent
  • Object data: The arbitrary data object that was passed to the upload()-function.

error

Fired on got an error.

  • First argument: Error object.
  • Second argument: Object with the following properties:
    • String uploadId
    • String name
    • Number size
    • String type
    • String uploadTo
    • Object data: The arbitrary data object that was passed to the upload()-function.

FAQ

Upload 0 bytes

Try to upload after "ready" event fired.

Browser Supports

This module uses FileReader API with ArrayBuffer, so make sure your browser support it.