Socket.io-file 2.0
Socket.io-file is now 2.0, much improved! See the below for details..! Also check the client client module too.
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
Example
You can found full source code here: Example Page Or Browserify Example
Server side
"use strict";const express = ;const app = ;const http = ;const httpServer = http;const io = httpServer;const SocketIOFile = ; app; app; app; app; io; httpServer;
Client side
var socket = ;var uploader = socket;var form = document; uploader;uploader;uploader;uploader;uploader; form { ev; var fileEl = document; var uploadIds = uploader; // setTimeout(function() { // uploader.abort(uploadIds[0]); // console.log(uploader.getUploadInfo()); // }, 1000);};
Socket.io-file 2.x File Upload Example Socket.io-file 2.x File Upload Example Select file and click upload button to upload. Multiple upload also supports.
API
constructor SocketIOFile(io socket, Object options)
Create new SocketIOFile object.
Available optionts:
- String uploadDir: String of directory want to upload. This value can be relative or absolute both. Or you can pass the object which has key as identifier, value as directory for multiple directories upload. Client can select the destination if server has multiple upload directories. Check the example to details.
- Array accepts: Array of string that refers mime type. Note that browsers and server can recognize different(like mp3 file, Chrome recognize as "audio/mp3" while server recognize as "audio/mpeg"). See the example to detail later. Default is empty array, which means accept every file.
- Number maxFileSize: Bytes of max file size. Default is undefined, means no limit.
- Number chunkSize: Size of chunk you sending to. Default is 10240 = 1KB. Higher value gives you faster upload, uses more server resources. Lower value saves your server resources, slower upload.
- Number transmissionDelay: Delay of each chunk transmission, default is 0. 0 means no delay, unit is ms. Use this property wisely to save your server resources with chunkSize.
- Boolean overwite: If sets true, overwrite the file if already exists. Default is false, which upload gonna complete immediately if file already exists.
- New from 2.0.1 String rename: Rename the file before upload starts.
- New from 2.0.1 Function rename: Rename the file before upload starts. Return value is use for the name. This option is useful to upload file without overwriting concerns. Check the details from later example.
Events
SocketIOFile provides these events.
ready (ADDED ON 2.0.12)
Fired on ready, means after synchronize meta data from client. Make sure upload after ready event triggered.
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 uploadDir: Directory for writing.
- Object data: An arbitrary data object that was passed to the client's upload()-function.
- New from 2.0.31 String originalFileName: When renamed file name, original name also delivers here.
stream
Fired on getting chunks from client. Argument has:
- String name
- String uploadDir
- Number size
- Number wrote: Bytes of wrote
- Object data: An arbitrary data object that was passed to the client's upload()-function.
complete
Fired on upload complete. Argument has:
- String name
- String uploadDir
- String mime: MIME type that server recognized.
- Number size
- Number wrote
- Number estimated: Estimated uploading time as ms.
- Object data: An arbitrary data object that was passed to the client's upload()-function.
- New from 2.0.2 String uploadId: Upload ID passing from Client.
- New from 2.0.31 String originalFileName: When renamed file name, original name also delivers here.
abort
Fired on abort uploading.
- String name
- String uploadDir
- Number size
- Number wrote
- Object data: An arbitrary data object that was passed to the client's upload()-function.
error
Fired on got an error.
- First argument: Error object.
- Second argument: Object with the following properties: -- String uploadId -- String name -- String uploadTo -- Object data: An arbitrary data object that was passed to the client's upload()-function.
destroy (ADDED ON 2.0.2)
Fired after destroyed Socket.io-file object
void SocketIOFile.prototype.destroy(void) (ADDED ON 2.0.2)
Destroy all resources that used in Socket.io-file. It also send some order to the client so that client can clear own resources.
Multiple uploading path
Socket.io-file supports multiple path upload. You can specify multiple upload path with passing object that has key as identifier, value as actual directory.
var uploader = socket uploadDir: music: 'data/music' document: 'data/document' ;
Remember, if you are using multiple path upload, client must select which want to upload.
uploader;
Rename before uploads
From version 2.0.1, you can now rename the file name before upload starts.
var path = ;var count = 0; var uploader = socket overwrite: false { var file = path; var fname = filename; var ext = fileext; return `_.`;});
Above example changes the filename before upload starts, adding counting value before file extension. This makes file always written, because filename never can't same(but only before you restart the server). If you want to generate some kind of unique identifier to the file always, consider to using Date string or other uid generating node modules like node-uuid. You can easily combine them together, just put in them into rename option.
From version 2.0.13, you can now just pass string directory, without function.
From version 2.0.31, when use rename property(or function), original file name will includes in argument of complete/start event.
var upload = socket rename: 'myNewFilename.xls';
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.