I need to add websocket functionality to a Swing desktop application, for duplex communication functionality. Most of the online examples cover webserver to browser use cases, not webserver to desktop application.
Two libraries I've discovered are Tyrus and TooTallNate/java-websocket. Would you recommend one of these two, or is there a better alternative?
In the example code I see that port to port communication is low level, requiring that custom objects be deconstructed down to their member fields for transmission (encoding/decoding). Moreover, any String values are not simply serialised, but are preceded by an int value denoting their length (so Strings sent as a pair of values).
Given all this extra overhead, compared to simply serialising and deserialising custom objects - using Apache serialize utils in combination with 'instanceof' - why would one typically opt for Netty over plain Java websockets?
Also, the example simply involved responding to a network request. I assume Netty allows connection sessions to be held open, too, so that the server can not only 'respond' but also 'push' data out to a client?
Netty looks interesting, but just wondering whether it is the right tool for the job. :-)
Yes, Netty is indeed a low level socket library, that is why is so versatile. It gives you full control over socket communication details, so ma not be the right tools for your job - ultimately, it depends upon what you need to do with websockets.
posted 2 months ago
My needs: I've developed a web application that models commercial harbour environments. To complement the webapp, I've developed a Swing desktop application. This application is more graphical, and gives a 'computer game' like bird's eye overview. Obviously for this desktop app to reflect the state of the webapp in realtime, the latter must have the ability to push data out to connected clients. This data is in the form of several custom object types - eg. VesselState.java.
You may also considerate to use JMS instead of websockets, at least if the refresh rate of data isn't crucial and a minimum delay can be tolerated.
You can decouple swing clients from application server by introducing a third layer, an MTOM like Apache ActiveMQ. This way, your appserver publish all the information clients need on a Topic in form of a Message - which content of course may be arbitrary - and the MTOM does the rest of the work, taking care to dispatch the messsage to all connected clients.
By the way, I gave a look at the websocket library you mentioned. I would choose Tyrus for two reasons: it's the reference implementation of websocket specification and it's used by Spring Framework under the hood for Spring's websocket client.
My two cents : always stay with standard solutions.
posted 2 months ago
Thank you Claude, you make good sense.
I've found some example code that seems to sit nicely with what you suggested...
Glad it helped! Another interesting feature that came to my mind about JMS is that MTOM softwares like Apache ActiveMQ usually offer an auto-reconnect feature out of the box, i.e if for some reason a client get disconnected, it could automatically try to reconnect without you having to deal with low-level details of handling that. Not sure if websockets offer the same.
posted 1 month ago
I promised to report back once I'd settled on a working solution for full duplex communication between server and desktop application, and I have. The vast majority of example code online is for chat room, where the client is a browser rather than a Swing application. I ended up using Tyrus standalone (Grizzly) on the client, and it is working very well for me.