Socket.IO Cookbook
eBook - ePub

Socket.IO Cookbook

  1. 184 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Socket.IO Cookbook

About this book

Over 40 recipes to help you create real-time JavaScript applications using the robust Socket.IO framework

About This Book

  • Create secure WebSocket connections and real-time mobile applications using Socket.IO
  • Devise efficient messaging systems between the server side and the client side
  • A step-by-step implementation guide to help you create elements of Socket.IO application

Who This Book Is For

If you have some knowledge of JavaScript and Node.js and want to create awe-inspiring application experiences using real time communication, then this book is for you. Developers with knowledge of other scripting languages should also be able to easily follow along.

What You Will Learn

  • Build rich and interactive real-time dashboards using Socket.IO to pipe in data as it becomes available
  • Design chat and multiple-person interfaces that leverage Socket.IO for communication
  • Segment conversations to rooms and namespaces so that every socket doesn't have to receive every message
  • Secure your data by implementing various authentication techniques, locking down the HTTP referrer and using secure WebSockets
  • Load balance across multiple server-side nodes and keep your WebSockets in sync using Redis, RabbitMQ or Memcached
  • Stream binary data such as audio and video in real-time over a Socket.IO connection
  • Create real-time experiences outside of the browser by integrating Socket.IO with hybrid mobile applications

In Detail

Socket.IO is a JavaScript library that provides you with the ability to implement real-time analytics, binary streaming, instant messaging, and document collaboration. It has two parts: a client-side library that runs in the browser, and a server-side library for node.js. Socket.IO is event-driven and primarily uses the WebSocket protocol that allows us to emit data bi-directionally from the server and the client. Socket.IO

This book is a complete resource, covering topics from webSocket security to scaling the server-side of a Socket.IO application and everything in between.

This book will provide real-world examples of how secure bi-directional, full-duplex connections that can be created using Socket.IO for different environments. It will also explain how the connection vulnerabilities can be resolved for large numbers of users and huge amounts of data/messages.

By the end of the book, you will be a competent Socket.IO developer. With the help of the examples and real-world solutions, you will learn to create fast, scalable, and dynamic real-time apps by creating efficient messaging systems between the server side and the client side using Socket.IO.

Style and approach

This book is written in a cookbook-style format and provides practical, immediately usable task-based recipes that show you how to create the elements of a Socket.IO application.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Socket.IO Cookbook


Table of Contents

Socket.IO Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Wiring It Up
Introduction
Creating a Node HTTP server with Socket.IO
Getting ready
How to do it…
How it works…
There's more…
Creating an Express server with Socket.IO
Getting ready
How to do it…
How it works…
See also
Using Socket.IO as a cross-browser WebSocket
Getting ready
How to do it…
How it works…
There's more…
See also
Debugging on the client
Getting ready
How to do it…
How it works…
There's more…
See also
Debugging on the server
Getting ready
How to do it…
How it works…
There's more…
2. Creating Real-Time Dashboards
Introduction
Loading static data from the server
Getting ready
How to do it…
How it works…
There's more…
Creating a server-side clock
How to do it…
How it works…
Loading data from MongoDB
Getting ready
How to do it…
How it works…
Real-time analytics
How to do it…
How it works…
There's more…
Handling connection timeouts
Getting ready
How to do it…
How it works…
3. Having Two-Way Conversations
Introduction
Creating a simple chat room
Getting ready
How to do it…
How it works…
There's more…
Managing the socket life cycle
How to do it…
How it works…
Emitting a private message to another socket
Getting ready
How to do it…
How it works…
Sending messages to all the sockets, except for the sender
How to do it…
How it works…
Building a multiplayer tic-tac-toe game
Getting ready
How to do it…
How it works…
4. Building a Room with a View
Introduction
Creating chat channels with namespaces
Getting ready
How to do it…
How it works…
Joining rooms
Getting ready
How to do it…
How it works…
Leaving rooms
Getting ready
How to do it…
How it works…
Listing rooms the socket is in
Getting ready
How to do it…
How it works…
There's more…
Creating private rooms
Getting ready
How to do it…
How it works…
Setting up a default room
Getting ready
How to do it…
How it works…
5. Securing Your Data
Introduction
Implementing basic authentication
Getting ready
How to do it…
How it works…
Performing token-based authentication
Getting ready
How to do it…
How it works…
Handling server-side validation
Getting ready
How to do it…
How it works…
Locking down the HTTP referrer
How to do it…
How it works…
Using secure WebSockets
Getting ready
How to do it…
How it works…
6. Performing a Load Balancing Act
Introduction
Performing load balancing with the Nginx server
Getting ready
How to do it…
How it works…
Using the Node.js cluster
Getting ready
How to do it…
How it works…
Using Redis to pass events between nodes
Getting ready
How to do it…
How it works…
Using Memcached to manage multiple nodes
Getting ready
How to do it…
How it works…
Using RabbitMQ to message events across nodes
Getting ready
How to do it…
How it works…
7. Streaming Binary Data
Introduction
Broadcasting an image to other sockets
Getting ready…
How to do it…
How it works…
There's more…
Uploading an image to the filesystem
Getting ready…
How to do it…
How it works…
Uploading an image to Amazon S3
Getting ready…
How to do it…
How it works…
Streaming audio
How to do it…
How it works…
Streaming live video
How to do it…
How it works…
8. Integrating with Mobile Applications
Introduction
Throwing an alert when the socket connects
Getting ready
How to do it…
How it works…
Pushing up data from the server
Getting ready
How to do it…
How it works…
Responding to tap events from the device
How to do it…
How it works…
Doing server-side pagination
Getting ready
How to do it…
How it works…
Triggering hot deploys
How to do it…
How it works…
Index

Socket.IO Cookbook

Copyright Ā© 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: October 2015
Production reference: 1081015
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-086-5
www.packtpub.com

Credits

Author
Tyson Cadenhead
Reviewers
Gonzalo Ayuso
George Brassey
Commissioning Editor
Amarabha Banerjee
Acquisition Editor
Reshma Raman
Content Development Editor
Mayur Pawanikar
Technical Editor
Siddhesh Ghadi
Copy Editor
Relin Hedly
Project Coordinator
Kranti Berde
Proofreader
Safis Editing
Indexer
Mariammal Chettiyar
Graphics
Disha Haria
Production Coordinator
Conidon Miranda
Cover Work
Conidon Miranda

About the Author

Tyson Cadenhead works as a senior JavaScript engineer at Aloompa in Nashville, Tennessee. He has dedicated his professional career to building large-scale applications in JavaScript and Node. Tyson addresses audiences at various conferences and programming meetups on how to build real-time web applications with Socket.IO or Meteor.js. He blogs on topics such as JavaScript and web technologies at http://www.tysoncadenhead.com.
Tyson lives in the greater Nashville area with his wife and two sons, where he enjoys gardening, raising chickens, reading philosophy and economics books, and playing guitar.

About the Reviewers

Gonzalo Ayuso is a web architect and specializes in open source technologies. He has more than 15 years of experience in web development. Gonzalo holds extensive experience in delivering scalable, secure, and high-performance web solutions to large-scale enterprise clients.
He has a varied background, especially in providing backend code. Gonzalo mainly focuses on Internet technologies, databases, mobile development, and programming languages, especially PHP, Python, and JavaScript. He blogs at http://gonzalo123.com. Gonzalo also likes to speak at technology conferences and organize coding dojos. You can also follow him on Twitter at @gonzalo123.
George Brassey is a developer from London. He loves art, music, and technology. George earned a degree in cinema studies from the New York University and...

Table of contents

  1. Socket.IO Cookbook

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Socket.IO Cookbook by Tyson Cadenhead in PDF and/or ePUB format, as well as other popular books in Computer Science & Application Development. We have over one million books available in our catalogue for you to explore.