21

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?

Community
  • 1
  • 1
Karthik
  • 4,950
  • 6
  • 35
  • 65

3 Answers3

7

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.

Tharif
  • 13,794
  • 9
  • 55
  • 77
Martin
  • 15,820
  • 4
  • 47
  • 56
  • plain websocket implementation supports binary data, I am aware of that. But I am specifically trying to find out if there is any hack/turnaround for `SockJS`. – Karthik Jul 24 '15 at 08:23
  • I know that we can use base64 encoding, but I was looking for alternatives, because this application has very frequent messages with attachments. So using base64 unnecessarily increases the size. – Karthik Jul 31 '15 at 06:37
  • But will it stream, or just encode in a one off to string action before sending. In other words, is it efficient to use WebSockets for this purpose? – Marc Jun 26 '16 at 17:01
0

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

daimon
  • 173
  • 1
  • 7
  • I think probably you misunderstood my question, I was not looking for something which supports binary, I am looking for a way to send binary data over SockJS. – Karthik Jul 30 '15 at 17:27
  • Why are you looking transfer binary data especially via SockJS , are you trying to support old browsers or another reason ? – daimon Jul 30 '15 at 20:20
  • Yes, I don't want to implement fallback options by my own. – Karthik Jul 31 '15 at 06:48
  • Can you check [Atmosphere](https://github.com/Atmosphere/atmosphere) ? I think, it could be alternative to sockjs. it is also support fallback scenarios . – daimon Jul 31 '15 at 15:11
  • Yes, that was my first thing I explored but I could not customize it according my need. May be I did not used it properly but somehow I did not feel comfortable with it. – Karthik Jul 31 '15 at 15:15
0

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.

Martin
  • 15,820
  • 4
  • 47
  • 56
  • this will not solve the problem right? I have to save the messages in the server. Offline messaging will not be possible with WebRTC. – Karthik Jul 31 '15 at 06:46
  • Yep. I don't know why you want to use websockets to send binary data if this is not a realtime application. Would make more sense to use multipart upload if you are queueing messages. – Martin Jul 31 '15 at 06:56
  • A user can chat with different users who are online through a websocket connectoin, If he wants send message an offline user I can use the same websokcet connection. Why should the client bother about whether the user is online or not? – Karthik Jul 31 '15 at 07:08