WebSocket Chat on WildFly and OpenShift (Tech Tip #51)

Posted by: Arun Gupta on October 29, 2014

Chat is one of the most canonical sample to explain WebSocket. Its a fairly commonly used interface and allows to explain the fundamental WebSocket concepts very easily. Of course, Java EE 7 WebSocket has one too, available here! You can easily run it on WildFly using the following steps:

curl -O http://download.jboss.org/wildfly/8.1.0.Final/wildfly-8.1.0.Final.zip
unzip wildfly-8.1.0.Final.zip
./wildfly-8.1.0.Final/bin/standalone.sh
git clone https://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples
mvn -f websocket/chat/pom.xml wildfly:deploy

And then access it at http://localhost:8080/chat/.

One of the biggest advantage of WebSocket is how it opens up a socket over the same port as HTTP, 8080 in this case. If you want to deploy this application to OpenShift, then WebSocket is available on port 8000 for regular access, and 8443 for secure access. This is explained in the figure below:

openshift-websocket-routing

 If you want to run this Chat application on OpenShift, then use the following steps:

  1. Click here to provision a WildFly instance in OpenShift. Change the name to “chatserver” and everything else as default. Click on “Create Application” to create the application.
  2. Clone the workspace:
    git clone ssh://544f08a850044670df00009e@chatserver-milestogo.rhcloud.com/~/git/chatserver.git/
  3. Edit the first line of “javaee7-samples/websocket/chat/src/main/webapp/websocket.js”from:
    var wsUri = "ws://" + document.location.hostname + ":" + document.location.port + document.location.pathname + "chat";

    to
    var wsUri = "ws://" + document.location.hostname + ":8000" + document.location.pathname + "chat";
  4. Create the WAR file:
    cd javaee7-samples
    mvn -f websocket/chat/pom.xml
  5. Copy the generated WAR file to the workspace cloned earlier:
    cd ..
    cp javaee7-samples/websocket/chat/target/chat.war chatserver/deployments/ROOT.war
  6. Remove existing files and add the WAR file to git repository:
    cd chatserver
    git rm -rf src pom.xml
    git add deployments/ROOT.war
    git commit . -m"updating files"
    git push

    And this shows the output as:
    Counting objects: 6, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (4/4), 6.88 KiB | 0 bytes/s, done.
    Total 4 (delta 1), reused 0 (delta 0)
    remote: Stopping wildfly cart
    remote: Sending SIGTERM to wildfly:285130 ...
    remote: Building git ref 'master', commit 05a7978
    remote: Preparing build for deployment
    remote: Deployment id is 14bcec20
    remote: Activating deployment
    remote: Deploying WildFly
    remote: Starting wildfly cart
    remote: Found 127.2.87.1:8080 listening port
    remote: Found 127.2.87.1:9990 listening port
    remote: /var/lib/openshift/544f08a850044670df00009e/wildfly/standalone/deployments /var/lib/openshift/544f08a850044670df00009e/wildfly
    remote: /var/lib/openshift/544f08a850044670df00009e/wildfly
    remote: CLIENT_MESSAGE: Artifacts deployed: ./ROOT.war
    remote: -------------------------
    remote: Git Post-Receive Result: success
    remote: Activation status: success
    remote: Deployment completed with status: success
    To ssh://544f08a850044670df00009e@chatserver-milestogo.rhcloud.com/~/git/chatserver.git/
    454bba9..05a7978  master -> master

And now your chat server is available at: http://chatserver-milestogo.rhcloud.com and looks like:

techtip51-websocket-chat-output

Enjoy!

Arun Gupta

About Arun Gupta

Arun Gupta is Director of Developer Advocacy at Red Hat and focuses on JBoss Middleware. As a founding member of the Java EE team at Sun Microsystems, he spread the love for technology all around the world. At Oracle, he led a cross-functional team to drive the global launch of the Java EE 7 platform through strategy, planning, and execution of content, marketing campaigns, and program. He is a prolific blogger since 2005 and have authored 1500+ blogs on technology. Arun has extensive speaking experience in ~40 countries on myriad topics and is a JavaOne Rockstar. He also founded the Devoxx4Kids chapter in the USA and continues to promoting technology education amongst kids. An author of a best-selling book, an avid runner, a globe trotter, a Java Champion, JUG leader, he is easily accessible at @arungupta.