Skip to content

Pushing data to multiple WebSocket clients from Java server

License

Notifications You must be signed in to change notification settings

ran-jit/websocket-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebSocket Demo

This is a basic example of WebSocket pushes the server’s time to all connected clients.

Server

The endpoint Service ("/ data") schedules the task and publishes the server time to the connected clients. The timer is implemented as lazy. So the timer is scheduled when the first client connects to the endpoint. After that, the message is sent by async to all the clients connected to every minute interval.

Client

The Web client is pretty simple. On the page load, the JavaScript code connects to the WebSocket endpoint on the server. The callback onMessage() is invoked when the message (server's time) arrives. In this callback, the current time is updated in UI. Here there is no page refresh, no heavy HTTP headers and, no AJAX long polling hacks either.

How to test?

  1. Run Driver.java.
  2. Enter http://localhost:8080 on your browser.

About

Pushing data to multiple WebSocket clients from Java server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published