Websockets support sending of blobs, but how do we send additional text data with them?
My use-case is a file uploader. I am slicing each file in multiple parts, and send max 3 parts at once to the server. On the server, I need a way to identify the order of these slices, so I can reconstruct the file. This is why I need to send the index of the slice along with blob, and the name of the file (multiple files can be uploaded)
The easiest way to send metadata with binary data is delimiting by size. You can generate a random number to identify a file (a 64 bit int or a UUID). Prepend this number to all file chunks, then prepend the chunk number (say 2 bytes). So you'll have a protocol that looks like this: first 8 bytes (or 16 if you choose UUID) are for the identifier then 2 bytes for the chunk number then the data. Send the metadata first as a normal JSON. Metadata will have the filename and the ID and may be other data you may want like the number of chunks. Of cause there can be much more ways to solve this.