Book Image

Websocket Essentials: Building apps with HTML5 websockets

By : Varun Chopra
Book Image

Websocket Essentials: Building apps with HTML5 websockets

By: Varun Chopra

Overview of this book

Table of Contents (13 chapters)
WebSocket Essentials – Building Apps with HTML5 WebSockets
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
6
Enhancing HTML5 Web Application Development Using Modern Tools
Index

Tips and tricks


Let's talk about some tips and tricks that you can use while making an application.

  • Using JSON:

    The JSON format is an easily readable format in JavaScript. It is always good to transfer data in the JSON format.

  • Object-based structure for WebSockets:

    Normally, in any application where we have to transfer different data sets, it is always preferred that we finalize a proper structure for the messages. Take the example of a chatting application: if we finalize a structure, it is better to handle the message. Here is a sample structure:

      {
        type: "message"
        data : {
          from: "varun"
          to : "user1"
          data : "hello"
               }
      }
  • Using ArrayBuffer:

    There is another way to send data using ArrayBuffer; you can also send a Binary Large Object (BLOB). Here is an example of the same:

    var array = new Float32Array(5);
      for(var i = 0; i < array.length; ++i) {
        array[i] = i / 2;
      }
      ws.send(array, { binary: true, mask: true });

These kinds of structures can help in...