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:
1
2
3
4
5
6
7
8
|
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:
If you want to run this Chat application on OpenShift, then use the following steps:
- 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.
- Clone the workspace:
123git clone ssh://544f08a850044670df00009e@chatserver-milestogo.rhcloud.com/~/git/chatserver.git/ - Edit the first line of “javaee7-samples/websocket/chat/src/main/webapp/websocket.js”from:
123var wsUri = "ws://" + document.location.hostname + ":" + document.location.port + document.location.pathname + "chat";
123var wsUri = "ws://" + document.location.hostname + ":8000" + document.location.pathname + "chat"; - Create the WAR file:
1234cd javaee7-samplesmvn -f websocket/chat/pom.xml - Copy the generated WAR file to the workspace cloned earlier:
1234cd ..cp javaee7-samples/websocket/chat/target/chat.war chatserver/deployments/ROOT.war - Remove existing files and add the WAR file to git repository:
1234567cd chatservergit rm -rf src pom.xmlgit add deployments/ROOT.wargit commit . -m"updating files"git push
1234567891011121314151617181920212223242526Counting 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 cartremote: Sending SIGTERM to wildfly:285130 ...remote: Building git ref 'master', commit 05a7978remote: Preparing build for deploymentremote: Deployment id is 14bcec20remote: Activating deploymentremote: Deploying WildFlyremote: Starting wildfly cartremote: Found 127.2.87.1:8080 listening portremote: Found 127.2.87.1:9990 listening portremote: /var/lib/openshift/544f08a850044670df00009e/wildfly/standalone/deployments /var/lib/openshift/544f08a850044670df00009e/wildflyremote: /var/lib/openshift/544f08a850044670df00009e/wildflyremote: CLIENT_MESSAGE: Artifacts deployed: ./ROOT.warremote: -------------------------remote: Git Post-Receive Result: successremote: Activation status: successremote: Deployment completed with status: successTo 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:
Enjoy!
I tried a few months ago exactly the same. It didn’t work and i see now why. I need to use the port 8000 for the websocket connection.
Thanks
Rudy
Have you followed some progress? I use a regular Tomcat7 Openshift instance and Websockets just simply don’t want to work (while they do locally). I wonder if I’m just fighting an unwinnable battle if OpenShift simply doesn’t really support Tomcat7 websockets (on out-of-the-box cartridges) ..
update: yeah it works pretty well! Don’t know what I did wrong.
Hi
Im not directly write about your blog, but you seem know about this thema !
I hope I may put a ask ?
My problem is the constructor from websocket server ! where do it ? ( most sample have no constructor, only in sample from openshift make new WSServer() blaaa ..
Ok, I use vaadin, gwt, wildfly …. websocket working fine ! but without call new WSServer() nevertheless don’t will by create new websocketserver…… (over CDI i don’t known much 😉
I need reference in my session from websocket but this created fully asynchron …..
I dont know where I need begin to search ! maybe you have a tip for my ?
Best Regards
Maria,
Can you ask your question at https://developer.jboss.org/en/wildfly?
Hello Arun,
I have develop just link de HOL Java EE 7, but my Websocket receive just after the connection an error code 1011.
A am using HTTPS connection and using wss protocol to connect to my Websocket.
Do you have any tip what could be missing?
Best regards,
Bruno
Bruno,
Are you using different ports for HTTP and WebSocket?
Arun
Hello Arun!
Yes, I am using HTTPS (443) to access the xhtml page and 8443 to connect with wss Websocket.
Best Regards,
Bruno
Hello Arun!
I just figured out what is the cause of this: when I made the change at web.xml just like your tip at http://blog.arungupta.me/securing-websocket-wss-https-tls-techtip50/, think this have blocked the wss:// as well.
Once I change the /* to /my-jsfs-pages/*, my WebSocket works back again.
Thank you for your attention and keep posting more tips about Java EE 7 (and 8 :-)!!
Cheers!
Bruno
Hi Sir,
Thank you for this tut., I wonder if this chat application can run even in openshits free account?
Regards,
Clicking on ‘Click here to provision … ‘ takes me to an OpenShift project that looks like it wants to deploy an instance of WordPress 4 with PHP and MySQL.
Is something broken, or am I misunderstanding where that link should lead?