0

There is a SocketServer which I need to connect, receive data and then push it back to clients (web browser).

I just need to confirm the approach. I will create a Socket connection which will keep listening for message inside a ServletContextListener. The message will be then be stored in ServletContext so that it's available to push it back to client browser.

  1. Should I create socket connection inside ServletContextLister? Or is there any other way to listen to socket?

  2. For pushing data back to client, I don't want to have a bi-directional communication. So should I go for SSE or Html5 WebSockets? Any java framework supporting SSE or WebSocket along with client side library?

Thanks

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Abdullah Shaikh
  • 2,567
  • 6
  • 30
  • 43

1 Answers1

0

I implemented this and its working fine. For server I went with Jetty 8 and used jetty's WebSocket.

Briefly how I implemented is, for listening to messages from SocketServer I implemented ServletContextListener which initializes Socket which will consume the messages.

When a user hits a url I add the WebSocket connection to a Collection and store that collection as ServletContext attribute. This WebSocket list is then accessed by the Socket which iterates and sends messages where whenever messages arrives. The message is then pushed back to all the connected clients.

Abdullah Shaikh
  • 2,567
  • 6
  • 30
  • 43