Reputation: 909
I need to have multiple (5~100) android clients taking pictures and simultaneously sending them to a nodejs server over a local network, the size of each picture being roughly 2~10mb. Because I have lots of clients uploading files at the same time and I need those operations to be as fast as possible websockets seem to be a good ideia since their overhead is smaller than the pure-HTTP one.
I'm kinda of lost on how to send the upload in chunks from the android clients and how to reconstruct those chunks in the nodejs server (I'm using socket.io). I found some examples but they appear to receive the entire file in a single chunk, which doesn't make sense with large files like the ones I'm expecting. I would appreciate even some abstract explanation or pseudocode.
Upvotes: 0
Views: 1095
Reputation: 19011
According to RFC 6455, one WebSocket binary message can consist of either (1) one binary frame or (2) one binary frame and subsequent continuation frames (see 5.4. Fragmentation for details). So, when you send big binary data from Android, divide the data into one binary frame and multiple continuation frames, and send them to the server one by one. The server will gather the divided frames and construct one binary message automatically.
To do this, you have to use a WebSocket client library that provides means to send continuation frames manually. nv-websocket-client (my work) is such a library.
The following code from README of nv-websocket-client is an example to send a text message ("How are you?"
) that consists of one text frame and two continuation frames. You can do the same for a binary message.
ws.sendText("How ", false)
.sendContinuation("are ")
.sendContinuation("you?", true);
Upvotes: 1