I am working on a messaging application using Spring websockets(STOMP as a sub-protocol) and Sockjs.
I should provide support to send files in messages.
According to this ticket, sockjs does not support binary data, but STOMP does.
I know that we can convert image to base64 and send it over stomp, but i think this is not the best practice as there is lot of conversion and overhead. Also I have to save the messages, So to save this base64 encoded files at server again I will have to decode them.
I have couple of questions :
1) Is there a workaround to send image/files over sockjs + stomp or converting to Base64 is the only way?
2) May be this a very silly question but according to this question it is possible to send binary data over STOMP(without sockjs). How difficult is it to support fallback without sockjs?
Thank you.
EDIT : If using base64 is the only option, I would rather make a POST request to save the messages which has attachments instead of using base64 encoding. Any ideas which is better?
Any Web Socket implementation will handle binary data if it is base64 encoded. This essentially serializes a binary stream to a string. All socket transports and wrappers can handle string data. Any Java base64 implementation should work.
On the browsers side base64 is handled natively in modern browsers with btoa()
and atob()
. If you support legacy browsers you may need a polyfill.
That said, if the Java server is just proxying messages between Web users, you won't need to decode the images in Java, you would just pass the string encoded images from one socket connection to another.