WebSockets in HTML5 and JBoss

Hi all, its a long time i put my pen in the blog, these days got little busy with my little kid, however i took little time to write about the next-generation client(browser) server communication using WebSockets. In the advent of HTML5 technology all the modern browser are capable of talking to latest servers using WebSockets API.

Web Sockets is a next-generation bidirectional communication technology for web applications which operates over a single socket and is exposed via a JavaScript interface in HTML 5 compliant browsers.

Once you get a Web Socket connection with the web server or an Application Server, you can send data from browser to server by calling a send() method, and receive data from server to browser by an onmessage event handler.

All the latest Web Servers and Application Servers has got implementation of WebSockets API as this is been included as part of JavaEE stack.

Let us look at the simple application which i created using JBoss Wildfly 8.1 and Maven


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">



once all the required and dependent jars got downloaded, let us code with our java server side handler class.

package com.spark.websockets.handler;

import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

 * @author Sony
public class WebSocketHandler {

	public String onMessage(String message){
		System.out.println("-------------------------> Received : "+message);
		return "Response from Server ";
	public void onOpenConnection(Session session){
		System.out.println("---------------------> Web Socket Connection Opened : "+session.getId());
	public void onCloseConnection(CloseReason closeReason){
		System.out.println("--------------> Close Reason : "+closeReason.getReasonPhrase());


Once we are done with our java handler, let us start with our html and java script as below.

        <title>HelloWorld Web sockets</title>
        <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
        <script language="javascript" type="text/javascript">
            var wsUri = getRootUri() + "/WebSocketsExample/hello";
            function getRootUri() {
                return "ws://" + (document.location.hostname == "" ? "localhost" : document.location.hostname) + ":" +
                        (document.location.port == "" ? "8080" : document.location.port);
            function init() {
                output = document.getElementById("output");
            function send_message() {
                websocket = new WebSocket(wsUri);
                websocket.onopen = function(evt) {
                websocket.onmessage = function(evt) {
                websocket.onerror = function(evt) {
                websocket.onclose = function (evt) {
            function onOpen(evt) {
                writeToScreen("Connected to Endpoint!");
            function onMessage(evt) {
                writeToScreen("Message Received: " + evt.data);
            function onError(evt) {
                writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
            function doSend(message) {
                writeToScreen("Message Sent: " + message);
            function cloaseConnection(){
            	writeToScreen("Closing Connection"+message);
            	websocket.close("closing socket !")
            function writeToScreen(message) {
                var pre = document.createElement("p");
                pre.style.wordWrap = "break-word";
                pre.innerHTML = message;
            window.addEventListener("load", init, false);
        <h2 style="text-align: center;">Hello World WebSocket Client</h2>
        <div style="text-align: center;">
            <form action="">
                <input onclick="send_message()" value="Send" type="button">
                <input id="textID" name="message" value="Hello WebSocket!" type="text"><br>
                <input onclick="closeConnection()" value="Close Connection" type="button">
        <div id="output"></div>

finally we are done with our coding part and ready for deployment, please deploy the war file in JBoss Wildfly 8.1 and hit http://localhost:8080/WebSocketsExample/ in the browser and start playing with web sockets.

Happy WebSockets, Happy Coding šŸ™‚


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s