HTML5 web sockets provides stable connectivity to servers. By using these web sockets we can setup a stable connection between browser and sever, and then we can send or receive messages.

Like shown in below image, users send and receive messages on real time

Observe below GIF, One is chrome and another is firefox, Whatever we type in chrome, that will be displayed in firefox.

Lets write java program for sharing editor

Java Program

To run below program, you need add java_websocket dependency. Here it is the maven dependency xml.

Once you install the above dependencies, run below program
import java.util.HashSet;
import java.util.Set;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

public class App extends WebSocketServer {

    private static int TCP_PORT = 4444;
    private String value = "";

    private Set<WebSocket> conns;

    public App() {
        super(new InetSocketAddress(TCP_PORT));
        conns = new HashSet<WebSocket>();

    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        System.out.println("New connection from " + conn.getRemoteSocketAddress().getAddress().getHostAddress());

    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        System.out.println("Closed connection to " + conn.getRemoteSocketAddress().getAddress().getHostAddress());

    public void onMessage(WebSocket conn, String message) {
        System.out.println("Message from client: " + message);
        value = message;
        for (WebSocket sock : conns) {

    public void onError(WebSocket conn, Exception ex) {
        if (conn != null) {
            // do some thing if required
        System.out.println("ERROR from " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
    public static void main(String[] args) {
        new App().start();

HTML Code 

This code contains textarea element. Whatever user types in that textarea, it will be sent to java web socket server. Java server will send that message to all its connections. That is how one user's text will be shown to all users. 
<!DOCTYPE html>
        .layout {
        textarea {
            border:1px solid #ccc;
        var ws = new WebSocket("ws://");
        var elm = document.getElementById('myTextArea');
        ws.onopen = function() {
        ws.onmessage = function (evt) {
            myTextArea.value =;
        ws.onclose = function() {
        ws.onerror = function(err) {
            alert("Error: " + err);
        function share(event) {
       <div class="layout">
        <h1>Sharing editor</h1> 
        <textarea id="myTextArea" onkeyup="share(event)" rows="100" cols="50"></textarea>


  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

  2. Very useful and interesting content.

    Quickbooks Expert
    quickbooks support phone number

  3. It provides stable connectivity to servers!
    yahoo mail sign up new user

  4. If any page has quality posts like yours then I will search for information very quickly. Please donate again
    ludo king

  5. This comment has been removed by the author.

  6. Thanks for your great and helpful presentation I like your good service. I always appreciate your post. That is very interesting I love reading and I am always searching for informative information like this.iot training institutes in chennai | industrial iot training chennai | iot certification chennai | iot training courses in chennai

  7. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    Check out : machine learning tution in chennai
    artificial intelligence and machine learning course in chennai
    best training insitute for machine learning



Follow this blog by Email

Popular Posts