Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Choosing which Java WebSocket implementation to use  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Team,

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?

Regards,

Chris.
 
Bartender
Posts: 1093
36
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at  Netty Project.. I don't know if Netty is better than the libraries you found, but  it's rock solid and supports a lot of socket-based protocols. Also, is actively developed.
 
Christopher Dodunski
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Netty indeed looks interesting.

I found an introduction to Netty here: https://www.baeldung.com/netty

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.  :-)

Regards,

Chris.
 
Claude Moore
Bartender
Posts: 1093
36
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christopher Dodunski
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Claude Moore
Bartender
Posts: 1093
36
IBM DB2 Java Netbeans IDE Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christopher Dodunski
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Claude, you make good sense.

I've found some example code that seems to sit nicely with what you suggested...

Server-side: https://www.baeldung.com/java-websockets

Client-side: https://youtu.be/X2R_Iyga88E

I'll start coding tomorrow and let you know how I get on.  :-)

Thank you, too, for advising on JMS.  I'll keep that in mind in case WebSocket proves problematic for some reason (for instance connectivity proves fickle).

Regards,

Chris.
 
Claude Moore
Bartender
Posts: 1093
36
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!