Monday, January 25, 2010

jwebsocket-Simple Java Based HTML5 WebSocket Server

I really like the idea of two-way full duplex communication between a user agent to a remote server, which is what defined by HTML5 WebSocket API. So I looked into it in more detail and i found it pretty interesting and i think it will be used widely for real time data transfer on the web. The Web Socket Protocol Document and Web Sockets API is pretty much enough to get the rough idea of WebSocket API.

jwebsocket - is a simple java based implementation of WebSocket Server with simple java API to build and run WebSocket server application. You can download the jars from the here.

Please note that this project is just for testing and playing with WebSocket Server API and not a production ready code. Here in this article i will just list out the steps to make and run a simple browser based chat application using jwebsocket server.

jwebsocket has a simple handler interface which an application has to implement to recieve WebSocket events like connection open, close and whenever it receives messages. Here's how it looks.
So here's the simple ChatHandler which implements WebSocketHandler

Here is the Eclipse Project to make things easy.

1. Import it as Java project in your workspace

2. Make sure that your build path is setup to include the jwebsocket jar

3. Default output folder should be bin

4. Select Run->Run Configuration

5. Select org.jwebsocket.core.server.StartWebSocketServer as Main Class

6. In Arguments tab add "-config websocket.xml" without quotes.

7. Hit Apply and Run

8. Go to http://localhost:8080/resource ,

This is it!! Please feel free to send me any issues you get i would be glad to resolve it


mandarinsoda said...

Well done! Now how about one in Scala, Clojure, Groovy and what else? Go?

WhiteHexagon said...

Sounds great, was trying to test it , but the jar manifest is missing the main-class definition...

WhiteHexagon said...

maybe you want to update the instructions for eclipse project:


so I'm up and running :) I see some extra characters prefixing the chats. I can email a screen shot if you like, or debug it if I find the complete sources and eclipse project :) (chromium osx java 1.6)
great work anyway!

Puran said...

yes i also noticed that i will try to fix it :) you can checkout the complete eclipse project from here

monadx said...

What would you change or improve to make this production ready? How many concurrent connections can it handle?


narup said...

we have been trying to develop it as communication channel. You can find more information here Also instead of using Mina I am trying on top of Netty now since it can handle more than 100,000+ concurrent connections.

monadx said...

sweet, thanks for the links!

any idea in mind when your improvement will be ready for tryout?

thanks once more!

Samir Bhatia said...

Hi Puran,

The eclipse project is not accessible any more. Also the link to web socket specification points to older version.

Gurkan Erdogdu said...

Siwpas, Simple Web Profile Application Server provides Web Socket Protocol Support starting from version 2.0.0. Check out for getting more information and trial download.

Hemantkumar Metalia said...

i am not able to download the code from specified link. can you please provide a working demo ?

Mehdi K said...

Could you please update the links to your code ?