Top Banner
Behind the scenes of Real-Time Notifications Cakefest 2015. New York, USA. Guillermo Mansilla @gmansilla
34
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Behind the scenes of Real-Time Notifications

Behind the scenes of Real-Time Notifications

Cakefest 2015.New York, USA.

Guillermo Mansilla@gmansilla

Page 2: Behind the scenes of Real-Time Notifications

What is real-time web?• The real-time web is a network web using

technologies and practices that enable users to receive information as soon as it is published by its authors, rather than requiring that they or their software check a source periodically for updates.

Wikipedia.

Page 3: Behind the scenes of Real-Time Notifications

A bit of history behind real-time web

• 1995: Java Applets.• 1996: Flash.• 1999: ActiveX extension called XMLHTTP, later

adapted by Mozilla, Safari and Opera as “XMLHttpRequest” (XHR).

• 2006: Comet.

Page 4: Behind the scenes of Real-Time Notifications

Comet: Implementations

• Hidden Iframe• XHR Polling• XHR Long Polling• JSONP Long Polling

Page 5: Behind the scenes of Real-Time Notifications

HTTP ProtocolClient Server

Request resource

Response

Page 6: Behind the scenes of Real-Time Notifications

Hidden IframeThis technique consists of having an Iframe receiving JavaScript code to be executed in the browser as events occur.

Two major flaws: • The lack of a reliable way to handle errors.• Impossible to track the state of the request

calling process.

Page 7: Behind the scenes of Real-Time Notifications

XHR PollingThis technique consists of making continuous requests to server, the server would keep sending back an empty response until there is something to be sent.

One major flaw: Unnecessary Requests can cause overload on the server side.

Page 8: Behind the scenes of Real-Time Notifications

Client Server

Request (Any updates for me?)

Empty Response (No updates for you, sorry.)

Request (Any updates for me?)

Empty Response (No updates for you, sorry.)

New updates!

Request (Any updates for me?)

Response (Yes, here are your updates.)

Page 9: Behind the scenes of Real-Time Notifications

XHR Long PollingUnlike XHR Polling, the server won’t respond immediately if there is no data to respond with. In other words, the request will be kept open for a while, once the server has something to be sent back the connection is closed and the client will emit a new request.

Page 10: Behind the scenes of Real-Time Notifications

Client Server

Request

No Immediate response

Request (Any updates for me?)

Response

Event (i.e. new updates)

Page 11: Behind the scenes of Real-Time Notifications

Real-world Example

Page 12: Behind the scenes of Real-Time Notifications

Facebook XHR-Long Polling Implementation

After loading facebook, open the network tab and take a look at the requests.

Notice how there is no immediate response... (hence the “Pending” status)

Page 13: Behind the scenes of Real-Time Notifications

Facebook XHR-Long Polling ImplementationClick on the request and then take a look at the “preview” tab

Page 14: Behind the scenes of Real-Time Notifications

Facebook XHR-Long Polling Implementation

After receiving a response a new request will be sent

Page 15: Behind the scenes of Real-Time Notifications
Page 16: Behind the scenes of Real-Time Notifications

HTML5 • Server-Sent Events (SSE).• Web Sockets.

Page 17: Behind the scenes of Real-Time Notifications

Server-Sent EventsSSE will let the users receive updates without explicitly asking for them continuously as we do with XHR long polling.

How?With a Javascript API that will let us create a stream over which the server can send events to the client.

Page 18: Behind the scenes of Real-Time Notifications

Server-Sent EventsClient:

Server:

Page 19: Behind the scenes of Real-Time Notifications

Server-Sent Events: Custom Events

Server: Emitting a custom event with the name userlogon

Client: Acting upon receiving an event with the name userlogon

Page 20: Behind the scenes of Real-Time Notifications

WebSocketsWebSockets is an advanced technology that makes it possible to open an interactive communication session between the user’s browser and server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

Mozilla Developer Network

Page 21: Behind the scenes of Real-Time Notifications

WebSocketsEstablishing the connection:

Some Callbacks:

Page 22: Behind the scenes of Real-Time Notifications

WebSocketsSending data:

Receiving data:

Page 23: Behind the scenes of Real-Time Notifications

WebSockets: Server Side.

One PHP option is “Ratchet”

Ratchet is a loosely coupled PHP library providing developers with tools to create real time, bi-directional applications between clients and servers over WebSockets. 

http://socketo.me/

Page 24: Behind the scenes of Real-Time Notifications

Node.js for Real-time applications

Why Node.js?

• Non-blocking I/O API.

• Event-driven.

Page 25: Behind the scenes of Real-Time Notifications

Socket.ioSocket.IO is a JavaScript library for realtime web applications. It enables realtime, bi-directional communication between web clients and server. It has two parts: a client-side library that runs in the browser, and a server-side library for node.js. Both components have a nearly identical API. Like node.js, it is event-driven.

Wikipedia

Page 26: Behind the scenes of Real-Time Notifications

Socket.ioSocket.IO primarily uses the WebSocket protocol with polling as a fallback option, while providing the same interface. Although it can be used as simply a wrapper for WebSocket, it provides many more features, including broadcasting to multiple sockets, storing data associated with each client, and asynchronous I/O.

Wikipedia

Page 27: Behind the scenes of Real-Time Notifications

Socket.io

Page 28: Behind the scenes of Real-Time Notifications

PHP – Node.js Integration

PHP

Redis / Memcached

Node.js

Session Data

Session Data

Page 29: Behind the scenes of Real-Time Notifications

Communication between PHP and Node.js

There are multiple ways of integrating PHP and Node.js. The following demo is one of the simplest one.

We will have the client to initiate a connection with socket.io to a node.js server, the client will wait for node.js to emit events and act upon them.

The PHP application will send messages to Node.js so that it can emit the proper events to the client.

Page 31: Behind the scenes of Real-Time Notifications

On the CakePHP side…

We will use an Event Listener to send a message to the Node.js when a particular event occurs.

I used the Blog Tutorial from the Cookbook as an starting point.

We’ll send a message to Node.js whenever an Article|Comment is added/edited.

Page 32: Behind the scenes of Real-Time Notifications
Page 33: Behind the scenes of Real-Time Notifications

Node.js…

Page 34: Behind the scenes of Real-Time Notifications

Client