It’s just data

Realtime Updates of Web Content Using WebSockets

Preface

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 https://github.com/rubys/websocket-demo.git
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:

header

  • 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
    request.reject();
    console.log((new Date()) + ' ' + url + ' connection rejected.');
    return;
  };

  // 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.');
  connections[url].add(connection);

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

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.

EZPZ!

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 = window.top.location.href

  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) => {
      callback(event.data);
    };

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

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

  // open (and keep open) the channel
  openchannel();
  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
app.post('/data.txt', (request, response) => {
 var fd = fs.openSync(dirname + '/data.txt', 'w');
 request.on('data', (data) => fs.writeSync(fd, data));
 request.on('end', () => {
   fs.closeSync(fd);
   response.sendFile(dirname + '/data.txt');
 })
})

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

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) {
      response.status(404).end();
    } else {
      marked(data, (error, content) => {
        if (error) {
          console.log(error);
          response.status(500).send(error);
        } else {
          response.send(content);
        }
      })
    }
  })
});

// watch for file system changes.  when data.txt changes, send converted
// markdown output to all /html connections.
fs.watch(dirname, {}, (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) {
              connection.sendUTF(content);
            }
          }
        }) 
      }
    })
  }
})

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:

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

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) => {
  response.json(stats(dirname));
});

fs.watch(dirname, {recursive: true}, (event, filename) => {
  var data = JSON.stringify(stats(dirname));
  for (connection of connections.json) {
    connection.sendUTF(data)
  }
})

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.

Recap

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. uaeassignmentwriting.help

Posted by uaeassignmentwriting.help 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.. https://www.snkcreation.com/high-da-free-video-submission-sites

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
SuperStupidFresh

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

Add your comment