Delivery.js (Experimental)
Bidirectional File Transfers For Node.js via Socket.IO
Sending files to the server, and pushing files to the client should be as easy as possible. Delivery.js uses Node.js and Socket.IO to make it easy to push files to the client, or send them to the server. Files can be pushed to the client as text (utf8) or base64 (for images and binary files).
Install
npm install delivery -g
Browser JavaScript
delivery.js can be found within lib/client.
Examples
Sending a File To a Server (Client is a browser)
Server-side code
var io = dl = fs = ; iosockets;
Client-side code
;
Pushing a File to a Client (Client is a browser)
Server-side code
var io = dl = ; iosockets;
Client-side code
;
Transfer files between two servers
Receive file
iosockets;
Send file
socket;
API
Server Functions
Importing delivery.js
dl = ;
Listen to the socket
var delivery = dl;
Listening to delivery.js events - delivery.on('event',fn)
delivery;
Sending a file
delivery;
delivery;
Server Events
'delivery.connect'
delivery.connect is called when a client connects to the server.
delivery;
'receive.start'
receive.start is called when the server starts receiving a file. The callback function takes a filePackage object that describes the file being sent.
delivery;
'receive.success'
receive.success is called once the file has been successfully reveived by the server. The callback function takes a filePackage.
delivery;
'file.load'
file.load is called after .send() is called and immediately after the file is loaded. The callback function takes a filePackage.
delivery;
'send.start'
send.start is called after .send() is called and immediately after the file begins being sent to the client. The callback function takes a filePackage.
delivery;
'send.success'
send.success is called after .send() is called and once confirmation is received form the client that the the file sent was successfully received. The callback function takes the uid of the file that was sent.
delivery;
FilePackage
FilePackage objects encapsulate files and includes a text representation (utf8), or base64 representation of the file. They also include the file's meta data, including name
, size
and mimeType
.
filePackage
returns true if the file has a corresponding mime type that is an image. It is possible that this method could return false if your file is an image, but does not have a mimetype, or does not have a mimetype of image/gif, image/jpeg, image/png, image/svg+xml, image/tiff. Look for var imageFilter
within delivery.js if you'd like to add additional mimetypes.
filePackage
returns true if the server used sendAsText()
.
filePackagetext
returns the text representation of the file sent. If the file has been base64 encoded it returns the base64 encoded version of the file.
filePackage
returns the base64 representation of the file prefixed with the data and mimetype necessary to display an image within <img src=''>
.
Client
Include delivery.js in your html file
Events
Client events mirror those on the server, see server events above for more details.
License
Delivery.js is released under the MIT license:
http://www.opensource.org/licenses/MIT
Road Map
- Incorporating feedback from other developers!
- Breaking files into pieces to help transfer larger files.
- md5 hash file and confirm the hash when a file has been received.
- ?