It’s just data

Realtime Updates of Web Content Using WebSockets


You've seen web sites with stock prices or retweet counts that update in real time. However, such sites are more the exception rather than the norm. WebSockets make it easy, and are widely supported, but not used as much as they could be.

Examples provided for WebSockets typically don't focus on the "pubsub" use case; instead they tend to focus on echo servers and the occasional chat server. These are OK as far as they go.

This post provides three mini-demos that implement the same design pattern in JavaScript on both the client and server.

Quick Start

For the impatient who want to see running code,

git clone
cd websocket-demos
npm install
node server.js

After running this, visit http://localhost:8080/ in a browser, and you should see something like this:


  • one
  • two
  • three

Server support

The primary responsibility of the server is to maintain a list of active websocket connections. The code below will maintain three such sets, one for each of the demos provided.

// attach to web server
var wsServer = new websocket.server({httpServer: httpServer});

// three sets of connections
var connections = {
  text: new Set(),
  html: new Set(),
  json: new Set()

// when a request comes in for one of these streams, add the websocket to the
// appropriate set, and upon receipt of close events, remove the websocket
// from that set.
wsServer.on('request', (request) => {
  var url = request.httpRequest.url.slice(1);

  if (!connections[url]) {
    // reject request if not for one of the pre-identified paths
    console.log((new Date()) + ' ' + url + ' connection rejected.');

  // accept request and add to the connection set based on the request url
  var connection = request.accept('ws-demo', request.origin);
  console.log((new Date()) + ' ' + url + ' connection accepted.');

  // whenever the connection closes, remove connection from the relevant set
  connection.on('close', (reasonCode, description) => {
    console.log((new Date()) + ' ' + url + ' connection disconnected.');

The code is fairly straightforward. Three sets are defined; and when a request comes in it is either accepted or rejected based on the path part of the URL of the request. If accepted, the connection is added to the appropriate set. When a connection is closed, the connection is removed from the set.


Client Support

The client's responsibitlity is to open the socket, and to keep it open.

function subscribe(path, callback) {    
  var ws = null;
  var base =

  function openchannel() {
    if (ws) return;
    var url = new URL(path, base.replace('http', 'ws'));
    ws = new WebSocket(url.href, 'ws-demo');

    ws.onopen = (event) => {
      console.log(path + ' web socket opened!');

    ws.onmessage = (event) => {

    ws.onerror = (event) => {
      console.log(path + ' web socket error:');
      ws = null;

    ws.onclose = (event) => {
      console.log(path + ' web socket closed');
      ws = null;

  // open (and keep open) the channel
  setInterval(() => openchannel(), 2000);

A subscribe method is defined that accepts a path and a callback. The path is used to construct the URL to open. The callback is called whenever a message is received. Errors and closures cause the ws variable to be set to null. Every two seconds, the ws variable is checked, and an attempt is made to reestablish the socket connection when this value is null.

First example - textarea

Now it is time to put the sets of server connections, and client subscribe function to use.

Starting with the client:

var textarea = document.querySelector('textarea');

// initially populate the textarea with the contents of data.txt from the
// server
fetch("/data.txt").then((response) => {
  response.text().then((body) => { textarea.value = body })

// whenever the textarea changes, send the new value to the server
textarea.addEventListener('input', (event) => {
  fetch("/data.txt", {method: 'POST', body: textarea.value});

// whenever data is received, update textarea with the value
subscribe('text', (data) => { textarea.value = data });

The value of the textarea is fetched from the server on page load. Changes made to the textarea are posted to the server as they occur. Updates received from the server are loaded into the textarea. Nothing to it!

Now, onto the server:

// Return the current contents of data.txt
app.get('/data.txt', (request, response) => {
 response.sendFile(dirname + '/data.txt');

// Update contents of data.txt'/data.txt', (request, response) => {
 var fd = fs.openSync(dirname + '/data.txt', 'w');
 request.on('data', (data) => fs.writeSync(fd, data));
 request.on('end', () => {
   response.sendFile(dirname + '/data.txt');

// watch for file system changes.  when data.txt changes, send new raw
// contents to all /text connections., {}, (event, filename) => {
  if (filename == 'data.txt') {
    fs.readFile(filename, 'utf8', (err, data) => {
      if (data && !err) {
        for (connection of connections.text) {

Requests to get data.txt cause the contents of the file to be returned. Post requests cause the contents to be updated. It is the last block of code that we are most interested in here: the file system is watched for changes, and whenever data.txt is updated, it is read and the results are sent to each text connection. Pretty straightforward!

If you visit http://localhost:8080/textarea in multiple browser windows, you will see a textarea in each. Updating any one window will update all. What you have is the beginning of a collaborative editing application, though there would really need to be more logic put in place to properly serialize concurrent updates.

Second example - markdown

The first example has the server sending plain text content. This next example deals with HTML. The marked package is used to convert text to HTML on the server.

This client is simpler in that it doesn't have to deal with sending updates to the server:

// initially populate the textarea with the converted markdown obtained
// from the server
fetch("/data.html").then((response) => {
  response.text().then((body) => { document.body.innerHTML = body })

// whenever data is received, update body with the data
subscribe('html', (data) => { document.body.innerHTML = data });

The primary difference between this example and the previous one is that the content is placed into document.body.innerHTML instead of textarea.value.

Like the client, the server portion of this demo consists of two blocks of code:

app.get('/data.html', (request, response) => {
  fs.readFile('data.txt', 'utf8', (error, data) => {
    if (error) {
    } else {
      marked(data, (error, content) => {
        if (error) {
        } else {

// watch for file system changes.  when data.txt changes, send converted
// markdown output to all /html connections., {}, (event, filename) => {
  if (filename == 'data.txt') {
    fs.readFile(filename, 'utf8', (err, data) => {
      if (data && !err) {
        marked(data, (err, content) => {
          if (!err) {
            for (connection of connections.html) {

The salient difference between this example and the previous example is call to the marked function to perform the conversion.

If you visit http://localhost:8080/markdown, you will see the text converted to markdown. You can also visit http://localhost:8080/ to see both of these demos side by side, in separate frames. Updates make in the window on the left will be reflected on the right.

No changes were required to the first demo to make this happen as both demos watch for file system changes. In fact, you can edit data.txt on the server with your favorite text area and whenever you save your changes all clients will be updated.

Final example - JSON

In this final example, the server will be sending down a recursive directory listing, complete with file names, sizes, and last modified dates. On the client, Vue.js will be used to present the data. We start with a template:

  <tr v-for="file in filelist">
    <td>{{ }}</td>
    <td>{{ file.size }}</td>
    <td>{{ file.mtime }}</td>

And add a bit of code:

var app = new Vue({el: 'tbody', data: {filelist: []}});

fetch('filelist.json').then((response) => {
  response.json().then((json) => { app.filelist = json });

subscribe('json', (data) => { app.filelist = JSON.parse(data) });

The first line associates some data (initially an empty array) with an HTML element (in this case tbody). The remaining code should look very familiar by now. Because of the way Vue.js works, all that is required to update the display is to update the data.

The server side should also seem pretty familiar:

app.get('/dir.json', (request, response) => {
});, {recursive: true}, (event, filename) => {
  var data = JSON.stringify(stats(dirname));
  for (connection of connections.json) {

Not shown is the code that extracts the information from the filesystem, the rest is the same basic pattern that has been used for each of these demos.

If you visit http://localhost:8080/filelist, you will see a table showing each of the files on the server. This list will be updated whenever you create, delete, or update any file. The server will push a new (and complete) set of data, and Vue.js will determine what needs to be changed in the browser window. All this generally takes place in a fraction of a second.

Vue.js is only one such framework that can be used in this way. Angular, Ember.js, and React are additional frameworks that are worth exploring.


By focusing on file system modified events, these demos have tried to demonstrate server initiated updates.

With comparatively little code, web sites can be prepared to receive and apply unsolicited updates from the server. The granularity of the updates can be as little as a single string, can be a HTML fragment, or can be arbitrary data encoded in JSON.

Reserving web sockets for server initiated broadcast operations can keep your code small and understandable. Traditional HTTP GET and POST requests can be used for all client initiated retrieval and update operations.

This makes the division of labor between the client and server straightforward: the server is responsible for providing state -- both on demand and as the state changes. The client is responsible for updating the view to match the state.

My friend recommended this blog and he was totally right keep up the good work

Posted by Programming Project Help at

Good way of telling, good post to take facts regarding my presentation subject matter, which i am going to deliver in my college

Posted by Electronics Engineering Homework Help at

Keep work up. You doing a right. Good luck

Posted by word cookies answersaz at

I must say we should have an online discussion on this. Digital Media Agency Karachi

Posted by Digital Media Agency Karachi at

I appreciate your efforts in preparing this post. I really like your blog articles.

Posted by dissertation help service at

Thank you for this sharing. great code.
Greffe capillaire Tunisie

Posted by greffe cheveux tunisie fue at

Definition: A socket is one endpoint of a two-way conversation link among two programs jogging at the community. A socket is certain to a port-wide variety so that the TCP layer can identify the software that facts is destined to be sent to. An endpoint is an aggregate of an IP deal with and a port-wide variety.

Posted by Economical Essay Writing at

This bankruptcy describes the Java API for WebSocket (JSR 356), which gives a guide for growing WebSocket packages. WebSocket is a utility protocol that gives full-duplex communications among peers over the TCP protocol.

Posted by Affordable Custom Essays Written at

Comparable two-way browser-server communications were done in non-standardized ways the usage of stopgap technologies consisting of a comet. The web socket protocol is currently supported in maximum fundamental browsers along with google chrome, Microsoft aspect, net explorer, firefox, safari, and opera.

Posted by Online Essay Help at

Great method for telling, great post to take realities with respect to my introduction topic, which I am will convey in my school

Posted by Do my Assignment Cheap at

A valid Group may be not practically continuously geographically near somebody alternately and only those same social web system. It’s regarding inclination associated What’s more answerable for what happens. Humankind is our extreme community, Also Everybody assumes An vital part. The unique ticket of the web might have been that it if be An community-oriented space the place you could speak through offering data.

Posted by Assignment Service at

All information you give is very beneficial for us thanks .
Tanda, Gejala Dan Ciri Penderita Penyakit Turun Berok (Hernia)
Agen QNC Jelly Gamat Kabupaten Buton Pasarwajo
Apakah Gagal Ginjal Bisa Sembuh Total ?
Pengobatan Gagal Ginjal Selain Cuci Darah
Ciri-Ciri Penderita Penyakit Sinusitis

Posted by Pantangan Bagi Penderita Maag Kronis at

I appreciate your endeavors Previously, get ready this post. I must say we ought to have a web examination or less group of journalists features masted Furthermore more distinction than difficult work, and speculative chemistry was. They expert over their subject, giving An prompt answer for practically intense activities with simplicity Also flawlessness. I truly like your blog articles.

Posted by Essay Writing at

At the point when month to month charges linger and the cash isn’t accessible, it can be distressing.Cash Advance ChicagoAuto Title Loans ChicagoPayday Loans Chula-vistaCash Advance Corona

Posted by cash advance at

I Appreciate This Work Amazing Post For Us I Like It. Nursing Assignment Help

Posted by Nursing Assignment Help at

Hi Buddy, Your Blog' S Design Is Simple And Clean And I Like It. Your Blog Posts About Online Writing Help Are Superb. Please Keep Them Coming. Greets! Online Operating System Homework Help

Posted by Online Operating System Homework Help at

Awesome and informative shared like such type of post  keep it up good work. It is also helpful a lot and can download PCN 501 GRAND CANYON ENTIRE COURSE loving  your Post . Bundle of Thanks  for such an awesome post.

Tutorials Expertsf

Posted by tutorialsexperts at

Comparable -manner browser-server communications had been executed in non-standardized methods the usage of stopgap technologies inclusive of a comet. I have to say we have to have a web exam or much less organization of journalists functions masted moreover greater distinction than tough work, and speculative chemistry becomes.

Posted by at

These are made with the finest gourmet chocolate in both the milk and dark varieties and are filled with exciting flavors like easter wishes images, caramel, champagne, amaretto, raspberry, orange, and pomegranate.

Posted by easter wishes images at

Apple Inc is leading the market when it comes to bringing up the new innovation. One of the best names when it comes to Technology

Apple Inc.
iMessage for Windows

Posted by imessage for windows at

Khaleej Apps harnesses the incredible power of the Android platform, allowing your brand tremendous versatility in user engagement, whether it’s a utility or game app. Our developers take advantage of some of the most vibrant app development tools that let them explore the magnificent open source android platform.

Posted by android app development dubai at

Thanks  for such an awesome post.

Posted by GoGoAnime at

This is very nice share with me

Posted by website designing company in Delhi at

Thank you for posting such an interesting article - it’s helped my business profoundly

Posted by Commercial Roofing Newcastle at

I’m thankful that you can simplify this otherwise complicated code

Posted by Office Cleaning Nottingham at

I’m thankful that you can simplify this otherwise complicated code

Posted by Office Cleaning Nottingham at

Most best dash cam 2018 have built-in screens so you can review video right on the device (though hopefully not while driving), while others have no screen at all, and must be paired with a smartphone app. While a dash cam can act as a safety device, one with a large 5-inch screen will take up more space on your

Posted by best front and rear dash cam at

Very useful and valuable details you shared, thanks for the important blog post. It helped me a lot.

Posted by Jina at

thanks for sharing good details..

Posted by Bappa at

For those who are not familiar with the term anime series here is a small explanation for you. Anime is a special Japanese animation art form from all genres found in movies.

Posted by Anime Haven at

The purpose behind this is a payday advance is just intended to enable you to take care of your expenses, on the off chance that you have a crisis, until your next payday.

Posted by Online Payday Loans san diego at

The purpose behind this is a payday advance is just intended to enable you to take care of your expenses, on the off chance that you have a crisis, until your next payday.

Posted by Online Payday Loans san diego at

Thanks for providing real time updates of web content

Posted by Jim Griffin at

Great Information are there in it.

Posted by Best Essay Help UK at

There is a good Information about Real time Web Content in WebSockets and It will help in Website.

Posted by Essay Writing Service at

What a really awesome post this is. Truly, one of the best posts I’ve ever witnessed to see in my whole life. Wow, just keep it up

Posted by web development companies in delhi at

This is very awesome post.Well given the information on the topic.Keep it up

Posted by Tampa dementia care services at

I am happy to comment on this

Posted by Piper at

Safety of the data is one of the most important things which every one of us inevitably needs to have an especially that which is related to the email accounts.

Posted by gmail login at

AGS Developers
Web Development Company
Hire Volusion Developers
Volusion Customization Services
eCommerce Product Data Entry Services
eCommerce Data Entry Services

Hire Volusion Developers
Volusion Customization Services
eCommerce Product Data Entry Services
Customized Tee Shirts
Create Custom Shirts

Posted by sdfs at

Psychology is the most crucial discipline which supports other disciplines as well. Students have to write essay assignments on the several topics of psychology which are assigned by teachers. Students Assignment Help provides the services of customer psychology assignment help to the students.

Posted by Assignment help | Assignment help online at

A superior  all assignment Help  reviews offered by this website with the advantage of online support with high proficiency level based on its latest research and information by professional reviews writers. Wide ranges of subjects are covered with separate writers for each subject.

Posted by All Assignment help review at

Really interesting

Posted by holly at

iMessage is essentially an Apple’s worked in texting administration. This administration causes you send content, picture, video, sound, and area simply like some other talking application. The most energizing thing about iMessage on PC is that it is just popular in iPhone and different devices of Apple.

Posted by Sara Martin at

Thank you for sharing with us, and we sincerely hope you will continue to update or post other articles

Posted by marek334 at

Many thanks for providing that code, cause I’ve rcently had troubles with realtime updates.

Posted by british essay writers at

It is really fantastic I liked it a lot,Thanks for sharing the post.
Wedding photographer in jaipur
Seo company in jaipur
call girl in jaipur

Posted by Wedding photographer in jaipur at

I entirely accept with the earlier audience. I think the an amazing motivation and great comments are great foundation to make high excellence material.

Posted by Rita at

I have forever thought about how to prepare top superiority items and release them on the weblog. These days I must state that the work is not for me. I keep it for professional people.

Posted by Penny at

Great piece! It is a absolute delight to browse it. Congratulation! Well-done! I am holding out for more texts.

Posted by Rita at

Hey everybody! It’s very enjoyable content. I really am shocked that you are so good. I didn’t esteem you!

Posted by Penny at

Congratulation on producing amazing content! I should declare that the review is a maximum standard articles and it meets all kinds of criteria. Well-done.

Posted by Lilly at

I price your undertakings antecedent, prepare this post. I ought to state we should always have an internet examination or less gathering of writers highlights spar additional more refinement than difficult work, and theoretical science was. They master over their subject, giving associate incite respond in due order concerning for all intents and functions exceptional exercises with straightforwardness conjointly faultlessness. I genuinely like your diary articles.

Posted by Custom Essay Writing at

Right now here is a light concern to inquire – how to create such a perfect post? It has almost all elements what make a good review. There is a getting name and fascinating information. Well done!

Posted by Ava at

I ought to admit that it really is one of the most interesting information I have actually look over. It really is also skilfully created. I’m an British teacher so I know what I mean.

Posted by Samantah at

Awesome post! Truly a pure fun to browse it. Congratulation! Well done! I’m holding out for more messages.

Posted by Penny at

Very good article! It’s a perfect enjoyment to review it. Congratulation! Well-done! I am looking for additional texts.

Posted by Autumn at

Hello everybody! It is very enjoyable content. I really am surprised that you are so qualified. I didn’t worth you!

Posted by Lilly at

Congratulation on creating fantastic information! I must think that the review is a maximum standard text and it matches all sorts of criteria. Well-done.

Posted by Rita at

There is a short problem to consult – how to establish this kind of excellent document? It presents almost all attributes what make a great content. Here is a getting title and interesting content. Well-done!

Posted by Ava at

I have got to declare that it really is 1 of the most fun articles I have actually study. It actually is also properly printed. I am an English instructor so I see what I suggest.

Posted by Rita at

An amazing document! Truly a genuine fun to read it. Congratulation! Well done! I’m waiting for more messages.

Posted by Samantha at

The article you shared here is great. This is really interesting information for me. Thank you for sharing!

Posted by windows 10 product key at

Most likely the best Hamachi elective that has been around for some time is Tunngle. Following the slogan “Made by Gamers. For Gamers” (wannabe Razer?), the Tunngle people group is contained the gaming scene as it were. hamachi alternatives

Posted by hamachi alternatives at

I like your post. It really useful with me. Thanks for sharing these useful information!

Posted by short life at

Commendable job with the article! It was really informative and enriching. Looking forward to more such posts in the future. Keep us updated with what’s in store! Will surely keep frequenting this website.
<a href= >Perdisco Myob </a>

Posted by aisha oberoi at

We are a one-stop essay writing service in the UK to get help from a professional essay writers and editors who know all the tricks of completing assignments with perfection.Our professional essay writers at are ready to help you with your entire essay writing needs!You can always expect top notch perfect essay help in UK, whenever you request for some.

Posted by Essay Writer at

The article you have shared here very good. This is really interesting information for me. Thanks for sharing!

Posted by hotmail login at

it’s really a great and useful piece of info. I’m happy that you shared this helpful info with us

Posted by Joe3434 at

Thank you for sharing such an informative article

Posted by Flat Roofing at

A great possibility for me and it was a superb knowledge to view this site. Very difficult to uncover these beneficial web page or web site.
product key

Posted by produkey at

Jiofi Local Html Router Login Password Username Change Settings. It is a  is a local domain name and change JIofi Wifi login Configuration

Posted by Jiofi Local Html at

Students Assignment Help serves the best corporate law assignment Help services to the students with the expert writers who are fluent in providing academic help writing at affordable price. We also ensure to cater original and plagiarism free contents.

Posted by Corporate law assignment at is one place for all academic assignment solutions therefore Singaporean students are ready to hire our expert writers for their online assignment help Singapore and achieve their target growth for academic success.

Posted by andrew symond at

On the off chance that you don’t have certainty to ponder individually, you are prescribed to search for an online coach. You can allude to the mentor when you confront issue in your examinations. When you have questions identified with GED, you can speak with the guide through messages or texting.

Posted by Admission Essay help UK at

Commendable job with the article! It was really informative and enriching. Looking forward to more such posts in the future. Keep us updated with what’s in store! Will surely keep frequenting this website.
<a href= >Make My Assignment</a>

Posted by aisha oberoi at

Students Assignment Help provides marketing assignment help services for all students at all the academic level. We possess more than 3000+ assignment writers from all around the world holding masters and PhD degree.  Our assignment expert writers serve assignment services at best price. They ensure students for timely delivery of the assignments before the deadline.

Posted by Marketing assignment at

Web Sockets allow you to open a two-way persistent connection between client and server, giving you the ability to push data back to the clients whenever data changes on the server without the client having to request it.

Posted by Star lord Jacket at

Web Sockets provide a persistent connection between a client and server that both parties can use to start sending data at any time. The client establishes a Web Socket connection through a process known as the Web Socket handshake. This process starts with the client sending a regular HTTP request to the server.

Posted by Walking Dead Clothing at

Those Who Come To Read Your Article Will Find Lots Of Helpful And Informative Tips

Posted by PHP Programming Homework Help at

I’m Really Impressed With Your Article, Such Great & Usefull Knowledge You Mentioned Here

Posted by Online Engineering Project Help at

Can this be used for building web based bots.

Posted by Instagram for Laptop at

Walmartone is the web portal, which is used by only the Walmart inc employees, Walmart Inc is a retail authorization of United States of America.

Posted by WalmartOne Login at

The more efficient the device will be, the higher the level of electricity consumption. Therefore, it is necessary to achieve an optimal balance between efficiency and power. In this matter, steam humidifiers lose considerably. Having a consumption of about 0.5 kW, with 12 hours of operation they will increase the cost of paying bills by 500 rubles. and more.The ultrasonic models, as well as steam modifications with a cold cooling principle, are less demanding. They consume energy in the range of 13-100 W. Therefore, their operation will cost about 15-100 rubles per month.

Posted by jeni johnson at

This is a great blog. I am pretty much impressed with your good work. You put really very helpful information. Keep it up.If you want norton support Then call our
Norton Internet Security Contact Number is +18555583999.

Posted by Norton Internet Security Contact Number at

This is really helpful post and very informative there is no doubt about it. I found this one pretty fascinating and it should go into my collection. Very good work! Get instant resolution for all technical concerns via phone. Contact +1-855-855-5142.
norton help,norton helpdesknorton support,norton support number,norton technical support,norton antivirus support,norton technical support number,norton customer support,norton tech support,contact norton support,norton support phone number,norton helpline number,norton help,norton helpdesknorton support,norton support number,norton technical support,norton antivirus support,norton technical support number,norton customer support,norton tech support,contact norton support,norton support phone number,norton helpline number,norton help

Posted by Norton Technical Support Service at

Awesome post. Very helpful article I am a regular visitor of your blog and appreciate you taking the time to maintain the excellent site

Posted by Quickbooks Error at


Very useful

<a herf="">Russian Girls Pic’s</a>

Posted by Diana Bubley at

Web sockets or eye sockets? Could someone here please explain what a web socket is and possibly compare it with an eye socket? Thank you.

Posted by betfair promos and codes at

All Assignment Help is an online Assignment Help and assignment writing service offering expert assignment help.Hire Australian,US assignment writing expert

Posted by Programming Assignment help at

Add your comment