Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy and access a Java WebSocket endpoint on Heroku without any additional frameworks

I have followed this very basic tutorial for setting up a WebSocket endpoint in Java: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html

Heroku, however, expects me to rely on Play Framework: https://devcenter.heroku.com/articles/play-java-websockets

My question is: how could I deploy the same without any additional frameworks and what procedure should I go through in order to make things work?

like image 413
coderodde Avatar asked Apr 15 '18 12:04

coderodde


People also ask

How do I deploy a Heroku WebSocket server?

Validate deploymentConnect the interactive client — using the name of your Heroku app instead of websockets-echo : $ python -m websockets wss://websockets-echo.herokuapp.com/ Connected to wss://websockets-echo.herokuapp.com/. > Great! Your app is running!

Can you use WebSockets with Heroku?

It provides a bidirectional channel for delivering data between clients and servers. It gives you the flexibility of a TCP connection with the additional security model and meta data built into the HTTP protocol. For more details on the WebSocket protocol refer to RFC 6455, which is the version supported on Heroku.


1 Answers

The problem you had was this:

The tutorial you followed was made for the GlassFish Application Server but Heroku only supports Tomcat 8 and Jetty. See here: https://devcenter.heroku.com/articles/war-deployment

But don't worry, I ported and tested the tutorial to run with Tomcat 8.

I also added the glassfish implementation of the javax.json specification. (Make sure to download the implementation and not the spec interfaces only) You can find it here: http://central.maven.org/maven2/org/glassfish/javax.json/1.0.4/

I also noticed why maybe your index.html didn't work locally: I think it was because the WebSocket URL was hardcoded in the websocket.js file. I have taken the liberty to fix this by making it dynamic.

Here is the complete NetBeans 8.0.2 project:
http://ray.hulha.net/WebsocketHome.zip

Here is the best way to create a war file from inside NetBeans 7 or 8:
enter image description here

There is one catch however, the Tomcat 8 on Heroku is missing the websocket addon.

But no worries, you can added it manually to the war file. Here are the steps:

  1. Download the Tomcat websocket addon here:
    http://central.maven.org/maven2/org/apache/tomcat/embed/tomcat-embed-websocket/8.0.29/tomcat-embed-websocket-8.0.29.jar

  2. The war file is really just a zip file. So I used 7-zip to open the war file. Then navigate to the WEB-INF/lib folder inside the war file.

  3. Copy the jar into the war. Drag and Drop the tomcat-embed-websocket-8.0.29.jar into the lib folder of the war file inside 7-Zip.

  4. Z-Zip will ask if you really want to copy the jar into the war file. Say Yes.

Here is the compiled war file complete with the tomcat-embed-websocket-8.0.29.jar ready to deploy on Heroku: http://ray.hulha.net/WebsocketHome.war

I used this command to deploy it:

heroku war:deploy WebsocketHome.war --app websockethome

Make sure to replace the app name in the end with your app name.

And here is the working result: http://websockethome.herokuapp.com/

enter image description here

like image 158
Ray Hulha Avatar answered Oct 29 '22 01:10

Ray Hulha