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?
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?
Share Improve this question edited May 23, 2017 at 12:14 CommunityBot 11 silver badge asked Jun 29, 2015 at 8:56 KarthikKarthik 5,0407 gold badges38 silver badges66 bronze badges3 Answers
Reset to default 7 +25Any 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.
Another alternative would be to use WebRTC only for sending of the binary images. One of the advanges to this is that these messages would be purely peer-to-peer. You would only need to implement this in your Web app and not in your backend. You would not then need to pay for the bandwidth used for the images. Unfortunately though, WebRTC is not supported by IE.
You have a look binaryjs . it is promise to Realtime binary streaming for the web using websockets.
lets check the examples
https://github.com/binaryjs/binaryjs/tree/master/examples
You should check Atmosphere(wasync), link will guide you to how to implement.
as far as understood your browser fallback scenario on client side you can use [base64 polyfill lib (https://github.com/davidchambers/Base64.js/) .
Best Regards