Book Image

JavaScript and JSON Essentials - Second Edition

By : Bruno Joseph D'mello, Sai S Sriparasa
Book Image

JavaScript and JSON Essentials - Second Edition

By: Bruno Joseph D'mello, Sai S Sriparasa

Overview of this book

JSON is an established and standard format used to exchange data. This book shows how JSON plays different roles in full web development through examples. By the end of this book, you'll have a new perspective on providing solutions for your applications and handling their complexities. After establishing a strong basic foundation with JSON, you'll learn to build frontend apps by creating a carousel. Next, you'll learn to implement JSON with Angular 5, Node.js, template embedding, and composer.json in PHP. This book will also help you implement Hapi.js (known for its JSON-configurable architecture) for server-side scripting. You'll learn to implement JSON for real-time apps using Kafka, as well as how to implement JSON for a task runner, and for MongoDB BSON storage. The book ends with some case studies on JSON formats to help you sharpen your creativity by exploring futuristic JSON implementations. By the end of the book, you'll be up and running with all the essential features of JSON and JavaScript and able to build fast, scalable, and efficient web applications.
Table of Contents (20 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Using JSON with Socket.IO


Setting up the Socket.IO server is simple. Here is the procedure to implement a real-time server that provides a continuous HTTP handshake, and continues to listen to requests via the Socket.IO framework.

We are going to demonstrate the implementation of JSON for real-time via a pinboard app. Any anonymous user who connects to the real-time server joins the session, and can view the pinboard and add whatever thoughts or whatever else that they like.

We are going to divide our application into two phases :

  1. Designing the board
  2. Implementing the real-time functionality via the Socket.IO library

Designing the board

In this phase, we are going to design a web interface through which our user can add or view the pins on the board. Remember how in Chapter 7, Alternate Implementations of JSON, we learned about template embedding? We are going to use a similar technique for our pinboard. So, let's start by creating a template file called index.html.js and inserting the following...