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.
<dependency>
  <groupId>org.java-websocket</groupId>
  <artifactId>Java-WebSocket</artifactId>
  <version>1.3.0</version>
</dependency>

Once you install the above dependencies, run below program
import java.net.InetSocketAddress;
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>();
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        conns.add(conn);
        conn.send(value);
        System.out.println("New connection from " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
    }

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

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

    @Override
    public void onError(WebSocket conn, Exception ex) {
        //ex.printStackTrace();
        if (conn != null) {
            conns.remove(conn);
            // 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>
<html>
    <head>
    <style>
        .layout {
            width:800px;
            margin:auto;
        }
        
        textarea {
            max-width:800px;
            width:80%;
            border:1px solid #ccc;
            font-size:14px;
            height:400px;
        }
    </style>
    <script>
        var ws = new WebSocket("ws://127.0.0.1:4444/");
        var elm = document.getElementById('myTextArea');
    
        ws.onopen = function() {
            console.log("Opened!");
        };
    
        ws.onmessage = function (evt) {
            myTextArea.value = evt.data;
        };
    
        ws.onclose = function() {
            alert("Closed!");
        };
    
        ws.onerror = function(err) {
            alert("Error: " + err);
        };
    
        function share(event) {
            ws.send(event.target.value);
        }
    </script>
    </head>
    <body>
       <div class="layout">
        <h1>Sharing editor</h1> 
        <textarea id="myTextArea" onkeyup="share(event)" rows="100" cols="50"></textarea>
       </div>
    </body>
</html>

50 comments:

  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.

    ReplyDelete
  2. Very useful and interesting content.

    Mask
    Quickbooks Expert
    quickbooks support phone number

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

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

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  8. Just seen your Article, it amazed me and surpised me with god thoughts that eveyone will benefit from it. It is really a very informative post for all those budding entreprenuers planning to take advantage of post for business expansions. You always share such a wonderful articlewhich helps us to gain knowledge .Thanks for sharing such a wonderful article, It will be deinitely helpful and fruitful article.
    Thanks
    DedicatedHosting4u.com

    ReplyDelete
  9. Buat kalian pemain pemula atau profesional tentunya membutuhkan teknik yang pas untuk menang dalam permainan AduQ Online
    asikqq
    dewaqq
    sumoqq
    interqq
    pionpoker
    bandar ceme terpercaya
    hobiqq
    paito warna
    forum prediksi

    ReplyDelete
  10. Really informative article.Really looking forward to read more. Really Cool.
    คาสิโนออนไลน์ที่น่าเชื่อถือและมีความเป็นมืออาชีพที่สุดในตอนนี้
    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร สล็อตออนไลน์ กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสล็อตออนไลน์ >>> Goldenslot
    สนใจร่วมสนุกกับ คาสิโนออนไลน์ คลิ๊กได้เลย
    มีทั้งคาสิโนออนไลน์ หวยออนไลน์ ฟุตบอลออนไลน์ สล็อตออนไลน์ และอื่นๆอีกมากมาย

    ReplyDelete
  11. Great. It is good to constantly coming up with creative ideas. Provides much needed knowledge. goal oriented blog posts and always tried to find creative ways to meet goals.

    Thanks
    Online affiliates

    ReplyDelete
  12. AppVN is a third-party app store for Android. Unlike Google Play, this app store provides you with some unique features, such as the facility to download certain official premium apps for free.
    https://appvn.one
    AppVN
    Appvn APK
    appvn ios
    appvn apk download

    ReplyDelete

  13. Your post is very good. I got to learn a lot from your post. Thank you for sharing your article for us. it is amazing post
    what is seo
    types of seo


    ReplyDelete
  14. This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
    Android Application development
    Web application

    ReplyDelete

  15. Get the most advanced Hadoop Course by Professional expert. Just attend a FREE Demo session.
    call us @ 9884412301 | 9600112302
    Hadoop training in chennai | Hadoop training in velachery

    ReplyDelete
  16. Nice Post...I have learn some new information.thanks for sharing.
    Data Analytics Courses in Mumbai

    ReplyDelete
  17. I`m working at html5 app development firm and it will surely help me out in my projects.

    ReplyDelete
  18. This is a wonderful article, Given so much info in it, Thanks for sharing. CodeGnan offers courses in new technologies and makes sure students understand the flow of work from each and every perspective in a Real-Time environmen python training in vijayawada. , data scince training in vijayawada . , java training in vijayawada. ,

    ReplyDelete
  19. The information you have posted is very useful. The sites you have referred was good. Thanks for sharing real time sharing editor using Java web sockets.
    Minneapolis Web Design

    ReplyDelete
  20. Best Fish Finders For The Money 2020 – Cheap & Budget Friendly
    Garmin Striker 4 Built-in GPS Fish Finder is found to be one of the best fish finder brand GPS. It’s comparatively small with other fish finders but still, it does its job perfectly. Each and everything will be clear to you due to its high colour contrasts and brightness. The good thing about it is you can mark your hot spots to make fish catching easier for you regardless of whether you are in open water or in the lake.
    https://bestfishfinderreviewz.com/

    ReplyDelete
  21. Best Water Based Polyurethane For Floors 2020
    Bona Mega Wood Floor Finish is a floor finish specially designed for wood floors. RUST-OLEUM Varathane Enhancing the natural beauty of the floor and retaining its original color, it provides maximum shine and makes your floor look good as new. This is a clear liquid that gives extra-ordinary clean and clear finish to the floors leaving it bright and sparkly.
    http://waterbasedpolyurethane.com/

    ReplyDelete
  22. Best Spray Tan Solution 2020
    A best spray tan solution is a method of giving you tanned skin for several days, without having to reveal yourself to deadly UV light as you would in a tanning salon or by sunbathing. There are several primary qualities that a tanning solution should have so that you attain natural-looking coverage and a great tanned color that not only shines up your complexion but also persists a pretty long time.Sunless tanners are available in lotion, gel, spray, powder, and mousse. There are also staged tanners for deeper and longer-lasting results.
    http://spraytansolutionreview.com/

    ReplyDelete
  23. Best 6 Volt Golf Cart Batteries In 2020
    Batteries are what powers the whole circuit process and are absolutely crucial for the machine; therefore you cannot risk getting a battery for your cart that Is undependable and erratic.Your golf cart is very essential because it is a mode of transportation, which is indeed, If not, the most revolutionary invention of mankind, providing all sorts of benefits including its efficiency and how less time consuming it makes our travels or journeys, and It does so with quite the comfort and ease.
    http://bestgolfcartbattery.com/

    ReplyDelete
  24. This is also a very good post which I really enjoyed reading. It is not every day that I have the possibility to see something like this .. what is cbd

    ReplyDelete
  25. A great piece of writing! You have an efficient flair for high quality writing. I am highly impressed by your way of writing this content beyond words.
    SAP training in Kolkata
    Best SAP training in Kolkata
    SAP training institute in Kolkata

    ReplyDelete
  26. I really have a lot of appreciation for your writing. Thank you so much for all your priceless input on this topic.
    SAP training in Mumbai
    Best SAP training in Mumbai
    SAP training institute Mumbai

    ReplyDelete
  27. Great post. I have learned a lot. Thanks.
    There are some discount drapes. beautiful indoor curtains, please try that.

    ReplyDelete
  28. I would like to say thank you for sharing the Informative article, this is a very Informative
    article about deep learning. You are doing a great job, keep it up.

    ReplyDelete
  29. I visited your site for the first time and started reading this one blog. It kept me stuck to your content. You have written it so well that I can’t keep my eyes off from it and reached the end of this.
    Explore the fashion

    ReplyDelete
  30. Really awesome blog to visit
    Like it so much
    Keep it up
    https://www.thrift-stores-near-me.blogspot.com

    ReplyDelete
  31. i am very impres of your post.

    https://grfreightservices.com/

    ReplyDelete
  32. Hello very nice work
    https://patchcracks.com/

    ReplyDelete
  33. Great post. I have learned a lot. Thanks.
    This will be very helpful for my Digital marketing blog

    ReplyDelete
  34. Hi I like your blog. There is a lot of good information on this blog, I loved reading it and I think people will get a lot of support from this blog
    Digital exhibition
    Online trade fair
    online events
    digital events
    digital trade fair
    Upcoming Online events
    upcoming exhibitions
    Explore Best Online Events

    ReplyDelete
  35. Real time sharing editor using Java web sockets. Your article is fascinating and tell you what? I have bookmarked your site and even shared to my friends. Thank you.
    I know of a school & fashion house that has a good education in fashion. Fashion training schools in accra Riohs Originate is a Creative Art Service and Training Institute in Ghana.
    They provide services and training for individuals/organizations that value their image, style, color and insist on promptness, accuracy and consistency.
    best fashion schools in accra ghana
    top fashion schools in accra

    fashion training schools in accra

    names of fashion schools in accra

    fashion design schools in accra ghana

    ReplyDelete
  36. I must thank you for the efforts you have put in writing this blog. I am hoping to check out the same high-grade content by you in the future as well.
    I was very pleased to uncover this website. I want to to thank you for your time for this fantastic read!! I definitely savored every little bit of it and i also have you saved as a favorite to see new things in your site.

    No more waste time to searching #HDimages, #PCwallpaper, #smartphone_wallpaper, #socialmedia_banner, #socialmedia_image_post, #natureHDimages, and many more awesome pictures. Just visit All Awesome Pictures Only For Love Hearts and choose your best choice.
    All Awesome Pictures Only For Love Hearts
    All Awesome Pictures Only For Love Hearts
    All Awesome Pictures Only For Love Hearts

    ReplyDelete
  37. This is the information exactly what I am looking on internet today. i learned useful information from this post. article is nicely explained and easy to understand. thanks for sharing this valuable information with us. keep your good work.

    ReplyDelete
  38. inhostpk Offers a variety of hosting services, from advanced VPS, Business hosting plans to beginners who want to start using shared hosting.
    Visit us 👉 https://inhostpk.com/

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts