Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Choosing which Java WebSocket implementation to use  RSS feed

 
Ranch Hand
Posts: 45
  • 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: 1114
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: 45
  • 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: 1114
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: 45
  • 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: 1114
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: 45
  • 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: 1114
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.
 
Christopher Dodunski
Ranch Hand
Posts: 45
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Claude,

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.

There is an excellent series of video tutorials where WebSocket messages may be of multiple type - rather more complicated than your typical chatroom service.  Moreover, each message type is encoded/decoded to/from JSON.  I was able to adapt this code to use with Tyrus rather than browser oriented JavaScript.  The author is Kevin Jones (sounds British), and here is a link to the first episode: https://youtu.be/bdFhsxOQRQ4

I'm happy to help anyone out with similar needs.

Regards,

Chris.
 
Claude Moore
Bartender
Posts: 1114
36
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for getting back to us and let us know you found a working solution !
 
Hang a left on main. Then read this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!