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?
This project is no longer maintained. If you encounter bugs with it or need enhancements, you can fork it and modify it as the project is under the Apache License 2.0.
In the Web browser with a custom WebSocketBy default, stomp. js will use the Web browser native WebSocket class to create the WebSocket. However it is possible to use other type of WebSockets by using the Stomp. over(ws) method.
The WebSocket API enables web applications to handle bidirectional communications whereas STOMP is a simple text-orientated messaging protocol. A Bidirectional WebSocket allows a web server to initiate a new message to a client, rather than wait for the client to request updates.
SockJS is a library that mimics the native WebSockets API. Additionally, it will fall back to HTTP whenever a WebSocket fails to connect, or if the browser being used doesn't support WebSockets. Like WS, SockJS requires a server counterpart; its maintainers provide both a JavaScript client library and a Node.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With