jWebSocket Step-By-Step – Part 1

This is the first part of the series of articles that i will write to cover the basics of jWebSocket Project, so that the users or developers can download the project and start using it either with the default features that comes with jWebSocket or with customized functionality on top of jWebSocket platform. Project jWebSocket is an open source Java and JavaScript implementation of the HTML 5 WebSocket protocol with a huge set of extensions. You can find more details about the architecture and different server concepts on our site http://jwebsocket.org. The aim of this article is to focus more on how to use it and get started and understand the various nitty gritty of jWebSocket usage.

So, how do i set it up quickly and run it?? Well, if you cannot wait at all then we have few demos in our product site that you can try without even bothering to set it up in your machine. But, if you want to try the thick and thing here are steps to follow:

Download the latest zip file jWebSocketServer-0.9.0.zip from here for the server and jWebSocketClient-0.9.0.zip from here. After you download and extract it you will see the folder structure like this.
jWebSocket Folder Structure
As you can see there are basically 4 folders
  • bin  – contains the batch file to run the server
  • conf – contains the configuration file ‘jWebSocket.xml‘, I will discuss more about it later.
  • libs – this folder contains all the jar files of different modules in jWebSocket, bundle jar and war for web application.
  • logs – folder that contains log settings.
Similarly for client, you will see the demos html file and javascript files. Now we have two options or modes to run jWebSocket

1. Standalone Server – Whichever mode you want to use, first set the environment variable JWEBSOCKET_HOME to the base location of your jWebSocket-0.9.0 folder(eg: /Users/puran/jWebSocket-0.9.0). To run as stand alone server, simply execute the batch/script file  from the bin directory of the jWebSocket folder. If every thing is honky dory your stand alone server should start successfully at port 8787. My Console looks something like this.

Now to test our stand alone jWebSocket server open any of the demo html files from jWebSocketClient-0.9.0 package you downloaded above. For example if I open file like file:///Users/puran/projects/jwebsockets/jWebSocket/jWebSocketClient-0.9.0/demos/chat/chat.htm then you should be able to login to the chat window as shown below.

2. jWebSocket embedded in Web Application – This option allows you to run jWebSocket Server as an web application in your Tomcat server so that it’s easier to integrate with other web applications.To do this simply copy the jWebSocketAppSrvDemo-0.9.0.war file from JWEBSOCKET_HOME/libs folder to the webapp folder of your tomcat installation. Now copy the jar file jWebSocketServer-Bundle-0.9.0.jar from the same libs folder to the lib folder of your tomcat. This single bundle jar bundles all the required libraries to run jWebSocket. Now at this moment we will not change any configuration whatsoever and just run with the default settings and configuration. Start your tomcat server and point your browser to http://localhost:8080/jWebSocketAppSrvDemo-0.9.0/. If everything is setup properly then you should see the home page with few demos.
So in this very first part we tried to get a quickstart run of jWebSocket. In the second part which will write shortly we will try to understand little bit more about our javascript client library and write our own simple client application that uses the default functionality of jWebSocket server. I hope all of you will find this article helpful in understanding very basics of jWebSocket. Please feel free to send feedbacks or any comments you have in mind.
18 comments
  1. guxxus said:

    Hi,i can’t run any demos of standalone mode or webapp mode (tomcat).JRE_HOME and JWEBSOCKET _HOME i set up like in example case.browser just can’t find html pages in case of standalone,but in the tomcat i received (The jWebSocket Application Server is successfully running.
    To put your own web site here please update the section within the pom.xml of the Maven project.) or (SYS > Connecting to jWebSocket Chat at ws://localhost:8787/;prot=json,timeout=360000…
    SYS < Disconnected from jWebSocket Chat.) in case of "jWebSocket Web Application demo 0.9.0",can you explain where i were wrong.browser chrome 5.0.375,thnx.

    • Hi, we know about that problem. AFAIK it is limited to Google Chrome when running from a local(!) file system (file:// protocol) and does not appear, when you load the pages from a web server like Apache – or Tomcat. This certain Chrome version even has problems with frames when loaded via file://. Please run the pages via the http:// protocol from any web server, e.g. Apache with http://localhost/jWebSocket. Did you try that already? Don’t hesitate to send us your further questions, we just kindly request you to post these to our forum at http://jwebsocket. Thanks in advance. Alex.

      • Homayun said:

        Thanks for this post. It’s working fine when we run with Mozila Firefox in http protocol.
        But when run with Google Chrome, showing Connecting and disconnect immediately:

        Here is the message: SYS > Connecting to jWebSocket Chat at ws://192.168.18.77:8787/;prot=json,timeout=360000…
        SYS < Disconnected from jWebSocket Chat.

        Would you please let us know the cause and appreciate if I get any advise.

    • Puran said:

      Sorry to reply late but to open the file locally you can delete these lines of code from the demo files

      var lFrameElem = this.frameElement;
      if( !lFrameElem ) {
      location.replace( “../../index.htm?page=demos/chat/chat.htm” );
      }

      these lines are right after tag. Note that this is only required if you are just trying to open a file locally and not from the server.

  2. mujoko said:

    Nice article but the when I try it on my laptop using 1. Standalone Server scenario its not really working.
    The server is running just like you say and the log show
    “2010-06-23 14:44:44,744 DEBUG – TCPEngine: Starting TCP engine ‘tcp0′ at port 8787″
    But when I try with Sample Chat. the page just display
    “SYS > Connecting to jWebSocket Chat at ws://localhost:8787/;prot=json,timeout=360000…”
    and never succeed to be logon.
    Thnx

  3. Greg said:

    Hi;
    Thanks for posting the step-by-step. In a vanilla install on a vanilla Tomcat 6 install, none of the websocket demos worked ; all imediately reported being “disconnected”. Any ideas?
    Thanks
    Greg

    • Greg said:

      OK, would help if I opened up port 8787…
      Works great now, well done guys.
      Greg

  4. sam said:

    0 down vote favorite

    I am able to deploy JWebSocketAppserver webapp to tomcat ver6.0 successfully, tried same steps to install it on jboss, following exceptions are thrown

    Deployment “vfs:///C:/jboss-6.0.0/server/default/deploy/jWebSocketAppServer-0.10.war” is in error due to the following reason(s): java.lang.ClassNotFoundException: org.jwebsocket.appserver.ServletBridge from BaseClassLoader@4bdccc{vfs:///C:/jboss-6.0.0/server/default/deploy/jWebSocketAppServer-0.10.war}

    Any idea

  5. Lizandra said:

    I need all the information possible about jwebsocket. Can you tell me where I can found this information? or any book o tutorial. My email is lcandelario@uci.cu

    • Currently all information we have is written in the how-to and developer guide section of our online-website at http://jwebsocket.org.
      We are about to rollout jWebSocket 1.0 this month and are working already on more detailed documentation which we will provide as soon as possible.
      Please do not hesitate to contact us in case of any concrete questions or suggestions. Your feedback is welcome.

      Thanks, Alex

  6. graziano said:

    I developed a test gwt application using smartgwt and jwebsocket. I deployed it in Tomcat6 where there is another webapp (jWebSocketAppSrvDemo-1.0) so i get the error “Starting engine ‘tcp0′ failed (WebSocketException: Address already in use)”. What is the best approach to have many webapps using jwebsocket in tomcat? I think every webapp needs own websocket server subsystem, with a separated jWebSocket.xml config file and difefrent ip addresses.

    • Yes, you can do that. The jWebSocketFactory.start() method allows to pass a reference to a .xml configuration file. You can create individiual configs and assign one per Tomcat app instance. You only need to assure that all apps listen on a separate port. The port can be configured in the .xml file. For further technical questions I kindly invite you to become a member of our forum, which is subject to more often support answers. Thanks in advance. Alex

  7. useless post. what about people want more than running their “bad structured” examples?

    • If you like to get some “jWebSocket Best Practices”, please let me know. We will support you in your projects and maybe even others can benefit from our conversation. We are open for your questions. Regards, Alex

  8. there is no standalone web application that we can deploy with maven, this is the problem. running examples or ready-to-deploy wars doesn’t make too much sense. it is difficult to get the standart functionality independently in your application. i’ve downloaded the full source codes but it has a parent pom so you have to build 35 examples at the same time. All i need is making the jwebsocket classes available for my project using maven and implement my own plugin. Ok the plugin is not a problem but making the jars available with maven is a hell. it never builds anyway.

  9. chris campos said:

    I get a weird error when trying any of the apps. I start the server fine. I put the .war file inside tomcat and start fine. I see the testpage just fine. When I try the apps “file:///C:/quaratine/websockets/web/index.html” and try hello world, I get this error on the server:
    Exception in thread “jWebSocket TCP-Engine (8787, non secured)” java.lang.NoClassDefFoundError: com/sun/net/ssl/internal/ssl/SSLSocketImpl
    at org.jwebsocket.tcp.TCPEngine.processHandshake(TCPEngine.java:341)
    at org.jwebsocket.tcp.TCPEngine.access$200(TCPEngine.java:54)
    at org.jwebsocket.tcp.TCPEngine$EngineListener.run(TCPEngine.java:445)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: com.sun.net.ssl.internal.ssl.SSLSocketImpl
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    … 4 more
    All environmental variables are set and I can echo them. Any help?

    • chris campos said:

      ahhh. Figured it out. JWebsockets does not like Java 7!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: