Realtime Web

With AngularJS + NodeJS

Zulfa Juniadi bin Zulkifli

zulfajuniadi@gmail.com

Links

4 Ways to do realtime

  • Short Polling
  • Long Polling
  • Server-Sent Events (EventSource)
  • Websockets

And how to do it in

nodejs + angularjs

Technology Stack

AngularJS

  • Front-End Framework
  • Backed by Google
  • Easy to learn

NodeJS

  • Javascript Runtime Engine
  • To build server and networking apps
  • Very very very fast even on high-loads
  • V8 by Google

Realtime Web

  • Responsive application == Happy users
  • Time Sensitive Apps
  • Collaboration
  • Tracking
  • Games
  • Chat

Short Polling

Poor-mans' Realtime Engine

How Short-polling Works

Short-polling Example

> node ex1.js 

Pros

  • Simplest implementation
  • Requests are short-lived
  • HTTP - Will probably work no matter what
  • Supported on all clients
  • Supported on all servers

Cons

  • Requests are short-lived - HTTP is expensive
  • Lag not the best user experience
  • Most requests return empty result
  • Cookies are sent together

To Use

  • You like request - response pattern
  • You want something that "Just Works"
  • You are afraid of long-running processes and memory leaks

Don't Use

  • Latency sensitive (Gaming)

Long Polling

Getting rid of excess fat

How Long Polling Works

Long-polling Example

> node ex2.js 

Pros

  • HTTP connection always on, less overhead
  • Almost no-lag
  • HTTP - Will probably work no matter what
  • Supported on all clients
  • Supported on all servers with modification

Cons

  • Requests are long-lived - check for memory leaks
  • Have to implement some sort of realtime DB or caching
  • Server has to be configured to handle these types of request
  • Uses up your AJAX quota

To Use

  • You want a responsive backend
  • You already have short-polling and need to scale fast and without infra change

Don't Use

  • You do a lot of AJAX
  • Your server has hard limits for max open connections
  • You are not comfortable with long-running process

Server Sent Events

Leaner than lean

How SSE Works

SSE Example

> node ex3.js 

Pros

  • Stateless HTTP connection always on, no HTTP overhead
  • HTTP - Will probably work no matter what
  • Auto negotiation and reconnection
  • Built-in queueing
  • Doesn't use up your AJAX quota
  • Supported on most clients
  • Supported on all servers with modification

Cons

  • Requests are long-lived - check for memory leaks
  • Have to implement some sort of realtime DB or caching
  • Server has to be configured to handle these types of request
  • Relatively newer than AJAX
  • Not supported on IE
  • Stateless

To Use

  • You want a responsive backend
  • Data is broadcast one to many
  • Bandwidth and server resources conscious

Don't Use

  • Your server has hard limits for max open connections
  • Data broadcasted is unique to each user
  • You are not comfortable with long-running process
  • Your end users are mainly running IE

Websockets

Latest & Greatest

How Websockets Works

Websocket Example

> node ex4.js 

Pros

  • Stateless connection always on, no HTTP overhead
  • Two-way realtime non-blocking socket
  • Supported on most clients

Cons

  • Requests are long-lived - check for memory leaks
  • Have to implement some sort of realtime DB or caching
  • Not all servers has implementation for WS
  • Newest technology to communicate realtime 
  • Some network devices does not WS passthru support (proxy, firewalls)

To Use

  • You want a responsive backend
  • Data goes both ways
  • Best usecase: MMORPG
  • Future applications

Don't Use

  • You are not comfortable with long-running process
  • You are not looking into revamping your whole system
  • Your network does not support it

I want the latest and greatest. It works for me

Websocket

I want to primarily do broadcasting

Server Sent Events

Ugh... No thanks, SSE

Long Polling

I just want it to work. I'm ok with lag

Short Polling

Thank you

Realtime Web

By Zulfa Juniadi Zulkifli