Monthly Archives: June 2013

WebSocket Chat application using JEE 7 with Glassfish 4.0

I was interested in WebSocket technology since last year. I have a posting related to WebSocket using Grizzly implementation. Since then, WebSocket becomes a part of JEE 7 release and Glassfish 4.0 with JEE 7 has been released. There are tons of articles that explains what’s changed in JEE 7 and how to start using JEE 7.

Experience

In this posting, I’d like to briefly discuss my experience with WebSocket implementation in JEE 7.

  • Very simple : using @ annotation, I can declare POJO into WebSocket Server/Client Endpoint really easiy.
  • There is a way of creating a WebSocket Server Endpoint without using @ annotation.
  • It seems I can not use both in one project. Programmatic WebSocket implementation overwrites the annotation implementation.
  • WebSocket URI gave me a hard time. After I figure it out, it is obvious. However, when I was in it, I really didn’t see. It took me a while to realize those WebSocket sample codes does not specify the webcontext name. This is critical piece. I was struggled with “undefine” error.
  • ws://domain:port#/webcontext(project name)/serverendpoint

Even though my WebSocket chat application is very simple application, I found it much easier to implement – compare with Grizzly implementation.

Idea

Web is evolving so fast and new ideas are keep pouring out. I think WebSocket will be very useful technology in terms of replacing point to point messaging. Especially, the connection has to be maintained constantly. One application that I used to maintain uses applet to satisfy this requirement. I think replacing an applet with WebSocket will be beneficial.

Resources

I presented this topic to my colleagues in the company Lunch & Learn session. Here is my presentation using Prezi.

Original Prezi Link HERE.

Source Code at GitHub is HERE.

Adding a project to GitHub – things to watch out

Not having much experience with Git, I made a watch out list, so I can refer back later without making a same mistake.

1. Do not add README.md when I create a new repository in GitHub. By adding README.md seems giving me a trouble later on.
2. Know the difference between git add -A vs. git add . (dot) vs. git add -u. Here is the difference.

  • git add -A stages All
  • git add . stages new and modified, without deleted
  • git add -u stages modified and deleted, without new

Below is the Git commands for my reference in the future.

msjo@MSJO-PC ~
$ cd gitrepo

msjo@MSJO-PC ~/gitrepo
$ ls
WebSocketJEE7Demo  WebsocketDemo

msjo@MSJO-PC ~/gitrepo
$ cd websocketjee7demo

msjo@MSJO-PC ~/gitrepo/websocketjee7demo
$ ls
WebSocketJEE7Demo

msjo@MSJO-PC ~/gitrepo/websocketjee7demo
$ touch README.md

msjo@MSJO-PC ~/gitrepo/websocketjee7demo
$ git init
Initialized empty Git repository in c:/Users/msjo/gitrepo/websocketjee7demo/.git
/

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$ touch README.md

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$ git add README.md

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$ git commit -m "first commit"
[master (root-commit) 4e202d8] first commit
 0 files changed
 create mode 100644 README.md

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$ git add websocketjee7demo

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$ git status
# On branch master
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#       WebSocketJEE7Demo/
nothing added to commit but untracked files present (use "git add" to track)

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$ git add -A
warning: LF will be replaced by CRLF in WebSocketJEE7Demo/WebContent/WEB-INF/fac
es-config.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in WebSocketJEE7Demo/WebContent/WEB-INF/gla
ssfish-web.xml.
The file will have its original line endings in your working directory.

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       new file:   WebSocketJEE7Demo/.classpath
#       new file:   WebSocketJEE7Demo/.project
#       new file:   WebSocketJEE7Demo/.settings/.jsdtscope
#       new file:   WebSocketJEE7Demo/.settings/org.eclipse.jdt.core.prefs
#       new file:   WebSocketJEE7Demo/.settings/org.eclipse.wst.common.component

#       new file:   WebSocketJEE7Demo/.settings/org.eclipse.wst.common.project.f
acet.core.prefs.xml
#       new file:   WebSocketJEE7Demo/.settings/org.eclipse.wst.common.project.f
acet.core.xml
#       new file:   WebSocketJEE7Demo/.settings/org.eclipse.wst.jsdt.ui.superTyp
e.container
#       new file:   WebSocketJEE7Demo/.settings/org.eclipse.wst.jsdt.ui.superTyp
e.name
#       new file:   WebSocketJEE7Demo/WebContent/META-INF/MANIFEST.MF
#       new file:   WebSocketJEE7Demo/WebContent/WEB-INF/faces-config.xml
#       new file:   WebSocketJEE7Demo/WebContent/WEB-INF/glassfish-web.xml
#       new file:   WebSocketJEE7Demo/WebContent/WEB-INF/web.xml
#       new file:   WebSocketJEE7Demo/WebContent/chatWindow.xhtml
#       new file:   WebSocketJEE7Demo/WebContent/js/jquery-2.0.2.min.js
#       new file:   WebSocketJEE7Demo/build/classes/endpoint/MyServerEndpoint.cl
ass
#       new file:   WebSocketJEE7Demo/src/endpoint/MyServerEndpoint.java
#

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$ git commit -m 'another commit'
[master 385faa3] another commit
warning: LF will be replaced by CRLF in WebSocketJEE7Demo/WebContent/WEB-INF/fac
es-config.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in WebSocketJEE7Demo/WebContent/WEB-INF/gla
ssfish-web.xml.
The file will have its original line endings in your working directory.
 17 files changed, 320 insertions(+)
 create mode 100644 WebSocketJEE7Demo/.classpath
 create mode 100644 WebSocketJEE7Demo/.project
 create mode 100644 WebSocketJEE7Demo/.settings/.jsdtscope
 create mode 100644 WebSocketJEE7Demo/.settings/org.eclipse.jdt.core.prefs
 create mode 100644 WebSocketJEE7Demo/.settings/org.eclipse.wst.common.component

 create mode 100644 WebSocketJEE7Demo/.settings/org.eclipse.wst.common.project.f
acet.core.prefs.xml
 create mode 100644 WebSocketJEE7Demo/.settings/org.eclipse.wst.common.project.f
acet.core.xml
 create mode 100644 WebSocketJEE7Demo/.settings/org.eclipse.wst.jsdt.ui.superTyp
e.container
 create mode 100644 WebSocketJEE7Demo/.settings/org.eclipse.wst.jsdt.ui.superTyp
e.name
 create mode 100644 WebSocketJEE7Demo/WebContent/META-INF/MANIFEST.MF
 create mode 100644 WebSocketJEE7Demo/WebContent/WEB-INF/faces-config.xml
 create mode 100644 WebSocketJEE7Demo/WebContent/WEB-INF/glassfish-web.xml
 create mode 100644 WebSocketJEE7Demo/WebContent/WEB-INF/web.xml
 create mode 100644 WebSocketJEE7Demo/WebContent/chatWindow.xhtml
 create mode 100644 WebSocketJEE7Demo/WebContent/js/jquery-2.0.2.min.js
 create mode 100644 WebSocketJEE7Demo/build/classes/endpoint/MyServerEndpoint.cl
ass
 create mode 100644 WebSocketJEE7Demo/src/endpoint/MyServerEndpoint.java

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$ git remote add origin https://github.com/mjtoolbox/WebSocketJEE7.git

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$ git push origin master
usage: git credential-cache [options]

    --exit                tell a running daemon to exit
    --reject              reject a cached credential
    --timeout          number of seconds to cache credentials
    --socket        path of cache-daemon socket
    --chain       use  to get non-cached credentials
    --username      an existing username
    --description   human-readable description of the credential
    --unique       a unique context for the credential

Username for 'https://github.com':
Password for 'https://mjtoolbox@github.com':
usage: git credential-cache [options]

    --exit                tell a running daemon to exit
    --reject              reject a cached credential
    --timeout          number of seconds to cache credentials
    --socket        path of cache-daemon socket
    --chain       use  to get non-cached credentials
    --username      an existing username
    --description   human-readable description of the credential
    --unique       a unique context for the credential

To https://github.com/mjtoolbox/WebSocketJEE7.git
 * [new branch]      master -> master

msjo@MSJO-PC ~/gitrepo/websocketjee7demo (master)
$

Conclusion

I can try to use UI tool to make it easier, but I feel that using Git command will enhance my experience in a long run.