7

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?

coderodde
  • 1,269
  • 4
  • 17
  • 34

2 Answers2

2

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

Ray Hulha
  • 10,701
  • 5
  • 53
  • 53
  • Also it seems the webapp-runner on Heroku does not support WebSockets. See this for a way to make it work: https://stackoverflow.com/questions/43170817/websocket-working-on-localhost-but-not-heroku-updated – Ray Hulha Apr 24 '18 at 14:16
  • Did not work out as expected. I tried to build your project and deploy the resulting WAR to Heroku. Then, I deployed **your** WAR file. None of the two did the job. – coderodde Apr 24 '18 at 14:56
  • Got it working ! You have to copy the tomcat-embed-websocket-8.0.29.jar into the war file. The war file is just a zip file. I used 7-ZIP. Inside the WAR file you have to go into the WEB-INF/lib folder. Copy the jar into this folder. I downloaded tomcat-embed-websocket-8.0.29.jar from here: http://central.maven.org/maven2/org/apache/tomcat/embed/tomcat-embed-websocket/8.0.29/ I also updated the war file on my server. Here is a working demo: http://websockethome.herokuapp.com/# – Ray Hulha Apr 25 '18 at 13:50
  • I added detailed steps for everything to my answer. – Ray Hulha Apr 25 '18 at 14:02
  • Thank you for your time, Ray! I will review/test your answer tomorrow. – coderodde Apr 25 '18 at 19:19
  • We are getting close, but, alas, when I rebuild and redeploy my version (with tomcat-emded-websocket-8.0.29 in WEB-INF\lib), Heroku shows me its "Application Error" page if I try to access its (dummy) index.html. In other words, deployment works, everything else does not. – coderodde Apr 26 '18 at 09:38
  • Are you on Skype ? – Ray Hulha Apr 26 '18 at 12:39
  • I am. When you are available? – coderodde Apr 26 '18 at 12:57
  • I have no microphone on my PC. – coderodde Apr 26 '18 at 14:16
  • Raymond, don't forget to mention that the JSON library JAR should be added too. – coderodde Apr 29 '18 at 13:47
0

Heroku does not require Play framework to use Websockets. That is just an example. As long as your app from the Oracle tutorial binds to $PORT it should work.

codefinger
  • 10,088
  • 7
  • 39
  • 51