3

I am building a java chatting web application.(Server and Client in one project)

follow is my condition

  • Springframework 4.2.3
  • JSP
  • Maven Project which converted from a Dynamic Web Project
  • Unable to use node.js

So User scenario in my head is

  1. User enters some text and press Send
  2. Ajax call to deliver messages to server
  3. Server checks the users who are currently connected(from Session maybe)
  4. Server calls other users script to append new message

I am quite confusing with step4. Is it possible that Java calls DOM event trigger?

How could a client get a new message event from server?

Thanks. :D

P.S. These days majority of chatting servers are event-driven. Is it possible to build an event driven chatting server with Java?

Juneyoung Oh
  • 7,318
  • 16
  • 73
  • 121

1 Answers1

2

Your solution is formed as if there is no WebSocket technology available to you. WebSocket is implemented to solve real time messaging issues. It pushes message to the destination.

But if to stick wih your method following is meaningful. You need someplace to keep incoming messages such as database or session.setAttribute [bad idea]. Then use some ajax call loop on the clients machine to ask for a new incoming message from server. Probably your server will slow down due to incoming flood of GET requests from multiple users.

to Your last question in post scriptum: yes, I use tomcat websocket api.jar in my projects. There is well written documentation on apache.org

As I mentioned, learn WebSocket if your users are not using old internet explorer browsers. There are bunch of tutorials on it...

Nako
  • 125
  • 7
  • Thanks. That was what I curious about. If you do not mind, I want to extra question. I found socket.io Java implementation, however most of sample are using apache websocket. Why people uses apache websocket more than socket.io?(I am trying to use socket.io maven jar, since I got experience with node.js chat server, however I could not get a hunch how to apply this technique to my java server-client web application) – Juneyoung Oh Dec 18 '15 at 11:18
  • Additionally, if it does not bothers you, I want to delay to choose an answer. I still expect to some implementation sample. – Juneyoung Oh Dec 18 '15 at 11:20
  • Yes, socket.io is more advanced than websocket. It is built on top of a websocket. There are bunch of functonalities written already for developers, but thats becoming another question. Your question was about triggering an event when message comes in. – Nako Dec 19 '15 at 06:31
  • There is an example represented by socket.io community https://github.com/socketio/socket.io/tree/master/examples/chat – Nako Dec 19 '15 at 06:34
  • I also have not experience socket.jar but this link should help: http://stackoverflow.com/questions/15568700/best-java-server-implementation-for-socket-io – Nako Dec 19 '15 at 07:40