A Step-by-Step Tutorial of Building a Simple Peer-to-Peer WebSocket App – Part 1

This tutorial series walks you through the simple steps of building an HTML5 WebSocket app, demonstrating the power of the publish/subscribe development pattern directly in JavaScript. Our sample is almost as simple as a Chat app, the “Hello World” app of the WebSocket world, but is a tad more visual and dynamic.

After trying out the completed application, you can either jump in the code and play with it right away, or move on to Part 2 of this tutorial for a step-by-step guide.

About the Environment

This tutorial requires a WebSocket server. We will use http://tutorial.kaazing.com, which hosts the JMS Edition of Kaazing WebSocket Gateway, Kaazing’s high-performance enterprise-grade WebSocket Server. All you’ll need to do is create a WebSocket connection to this server directly from your browser and start “talking” (read: send messages) to it from your browser. On the client side, you can use any modern browser, supported by JSFiddle (learn more about JSFiddle).

Getting Started

Firstly, to understand how the tutorial environment in JSFiddle works, open up the completed demo in JSFiddle. If you’re using a tablet or a smart phone, use the full page mode of the demo.

Note: The sample code uses an HTML5 element, <input type=”range”> that is unfortunately not supported by every browser. At the time of writing this article, the current version of Firefox, Firefox 14 doesn’t have support for it. Take a look at caniuse.com for detailed report on browser support.

JSFiddle provides a simple, integrated environment to edit the source of your web pages and apps. It has four panes:

  • Top-left: HTML code (very minimal HTML code in our demo)
  • Bottom-left: JavaScript code
  • Top-right: CSS code (empty in our case)
  • Bottom-right: Result

In the bottom right pane, you can interact with the completed app. Using the slider, you can change the size of the image. Now, open another browser window, pointing to the same JSFiddle demo URL. You can also use a tablet or a smart phone. Position your two browser windows side-by-side. As you adjust the slider in the bottom-right pane, right above the image in one browser window, the slider position and image size change in the other one. They’re kept in sync.

The browsers are communicating with the WebSocket gateway which is running in Amazon’s EC2 cloud, hosted in North Virginia, USA. When you move the slider, messages travel from your browser, to the WebSocket gateway, and back to the other browser.

Watch this video demonstrating the completed application (if the letters are blurry, switching the video to HD helps):

At this point, you can make any changes to the application. Click the Run button to test out your changes. To roll back to the original state, simply reload the original JSFiddle demo URL.

Note: If you see the image resize without you moving the slider, there’s a chance that somebody else is experimenting with the tutorial as well. To ensure that you’re not interfering with anybody else, change the topic name to something unique to you, for example by appending your name. Locate the following line in the bottom-left pane, towards the top of the file:


var TOPIC_NAME = "/topic/myTopic";

Modify the name of the topic. For example:


var TOPIC_NAME = "/topic/PeterTopic";

Test your code by clicking the Run button.

As another test, you can also change the application’s logging behavior. To give it a try, change the value of the IN_DEBUG_MODE variable to false, located in the bottom-left pane towards the top of the file. Original line:

var IN_DEBUG_MODE = true;

After the changes:

var IN_DEBUG_MODE = false;

Test your code by clicking the Run button.

In Part 2 you can learn about the demo code and how quickly and easily you can build real-time web applications with Web messaging.

This entry was posted in html5, Kaazing, WebSocket and tagged , . Bookmark the permalink.

6 Responses to A Step-by-Step Tutorial of Building a Simple Peer-to-Peer WebSocket App – Part 1

  1. Pingback: A Step-by-Step Tutorial of Building a Simple Peer-to-Peer WebSocket App – Part 2 | The Zinger

  2. Pingback: A Step-by-Step Tutorial of Building a Simple Peer-to-Peer WebSocket App – Part 3 | The Zinger

  3. Pingback: A Step-by-Step Tutorial of Building a Simple Peer-to-Peer WebSocket App – Part 4 | The Zinger

  4. Pingback: A Step-by-Step Tutorial of Building a Simple Peer-to-Peer WebSocket App – Part 5 | The Zinger

  5. Pingback: A Step-by-Step Tutorial of Building a Simple Peer-to-Peer WebSocket App – Part 6 | The Zinger

  6. Pingback: A Step-by-Step Tutorial of Building a Simple Peer-to-Peer WebSocket App – Part 7 | The Zinger

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s