Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Javascript binary file to server

Javascript binary file to server

Problem

Im trying to figure out what a highly scalable solution (serving multiple users of 10000+) would be.

Goal: What I want to achieve is to write a stream of mouse Coordinates to a binary file to a Server, i.e. the server is directly saved to the server from the userinteraction once stream is closed. The coordinates should be pushed every 20ms (about 50fps) to create a close representation of the mouse movement.

a) I know that nodeJS can be used to do a writestream, but I am not sure if such a high frequency of updates can be handled by such a structure - also if this is done by multiple users, this approach could fall apart.

b)The other possibility would be to locally write the file into binary and then upload it to the server afterwards.

Can anyone comment on the capabilities of these approaches and if there is another method that can be used?

Problem courtesy of: toxicate20

Solution

Based on your comment that the server does not need the data in real-time, you should definitely not send the data to the server every 20ms. Save it on the client and send it in chunks, say every 30 or 60 seconds. This can be done in memory in an array. Saving Mouse Coordinates is not very intensive.

Once you're ready to send it, I think you'll find XMLHttpRequest sufficiently fast enough for your needs.

This demo shows how to capture to coordinates and shows that 50fps is doable (I get up to 60fps in Chrome on OS X).

Demo: http://jsfiddle.net/ThinkingStiff/2Ls3A/

var coordinates = [],
    count = 0,
    SECONDS = 1000,
    INTERVAL = 5; 

window.onmousemove = function ( event ) {

    //move cursor over page for at least 10 secs for an accurate reading
    coordinates.push( [event.pageX, event.pageY] );

};

window.setInterval( function () {

    console.log( ( coordinates.length - count ) / INTERVAL + 'fps' );
    count = coordinates.length;

}, INTERVAL * SECONDS ); 

If you really think you need more efficiency in the sending of the data, the lowest-latency method to connect a client and server computer over the internet is a persistent TCP socket. This is available in HTML through the WebSocket API.

There are libraries in just about every server-side language that handle the end-point using the ws: URL schema (wss: for secure), socket.io for example.

Client:

var socket = new WebSocket('ws://example.com/endpoint' );

socket.onopen = function () {

    socket.send( 'send some text' );

};
Solution courtesy of: ThinkingStiff

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Javascript binary file to server

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×