Implementation of facebook like chat on Java WebApp with Javascript

1.3k views Asked by At

I am new in XMPP but I need to make facebook like chat on web application. the application is a webapp written with Java EE 7 with JSP, struts2, tiles, using Tomcat server.

I was reading something about how to implement that feature, i found few solutions the best (because it ist the easiest) to use in my opinion is: JavaScript strophe and Bosh technology. I would like to connect with Bosh from java after authorisation in web application and next, I planning to use bosh session to connect with bosh from Strophe.

My main question is: will implementation will work when visitor will changing sites? eg. when he is on main page and chege to settings. And will Bosh session persist all the time user will be logged in webapp?

I would not to use WebSockets and Atmosphere because the fact that I would need to made many changes in project and I think I would not cope with that.

Have you any suggestions if it is good idea? Thanks for help!

Edit:

I decided to do it with Atmosphere, I using tomcat with java ee7

first i trying to run any atmosphere example. When i run it on tomcat as a new application it works, but when i adding it to my app with struts I've got a problem probably with web.xml My files looks like this:

web.xml:

    <!-- Atmosphere -->
    <servlet>
        <description>AtmosphereServlet</description>
        <servlet-name>AtmosphereServlet</servlet-name>
        <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
        <async-supported>true</async-supported>
        <!-- Use it with mvn jetty:run -->
        <init-param>
            <param-name>org.atmosphere.cpr.packages</param-name>
            <param-value>org.atmosphere.samples</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
  <!-- Atmosphere Mapping-->
   <servlet-mapping>
        <servlet-name>AtmosphereServlet</servlet-name>
        <url-pattern>/chat/*</url-pattern>
    </servlet-mapping>

    ...     

    <!-- Filters -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.comarch</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>

    ...

without first part (with Atmosphere, everything works fine)

next part is websocket class which is taken from examples: https://github.com/Atmosphere/atmosphere-samples/blob/master/samples/websocket-chat/src/main/java/org/atmosphere/samples/chat/WebSocketChat.java

I've got website looks like: github.com/Atmosphere/atmosphere-samples/blob/master/samples/websocket-chat/src/main/webapp/index.html

and my application.js looks like:

https://github.com/Atmosphere/atmosphere-samples/blob/master/samples/websocket-chat/src/main/webapp/jquery/application.js

I changed 15 line to direct link:

var request = { url: 'http://localhost:8081/chat/',

when I go to my website, I can see error in my console:

WebSocket connection to ws://localhost:8081/chat?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=2.1.2-jquery&X-Atmosphere-Transport=websocket&X-Cache-Date=0&Content-Type=application%2Fjson&X-atmo-protocol=true failed: Error during WebSocket handshake: Unexpected response code: 501

1

There are 1 answers

0
Mark S On

Strophe will work if you are okay with doing it in JavaScript. It's up to you to decide what's best for your site. Do your initial auth in Java on the back end and pass the session info out to Strophe to then attach with. You can re-attach to an existing Bosh session when a user changes pages (you must keep track of the RID between pages).

See: http://metajack.im/2008/10/03/getting-attached-to-strophe/ for an example.