Top Banner
Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop
12

Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

Dec 27, 2015

Download

Documents

Suzan Hines
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: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

1

Real-time Web Application with Node.js

CENTRE FOR NETWORK RESEARCH

COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY

Aj. Suthon, Nong Gun, Nong Pop

Page 2: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

2

Case Study: Registrar

● Too many queries● Too busy to write● Slow response

● Improve byo Carefully design workflowo Find better technologies

Page 3: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

3

One Possible solution● Node.js - high performance

o 8000+ concurrent connection per CPU core

● Socket.ioo real-time update to Web Cliento 2000+ concurrent connection per CPU core

● Rediso For cachingo For publish/subscribe (PUB/SUB)

● Angular.js (to implement SPA)

● REST (to work with others like PHP)

Page 4: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

4

CACHING

Read data Course data

struct type STRING

Struct type HASHEnroll data

Page 5: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

5

PUBLISHING

3. REST API

4. Save data

ACID

5. Update Enroll data

1. request for

Sub

2. Sub

6. Pub

7. Socket IO update Enroll

Page 6: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

6

Activity 1: Setup

Git

Node.js

Easy PHP (VC distribution might be needed)

npm (installed by Node.js)

bower (run command >> npm install -g bower)

Page 7: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

7

Activity 2: Prepare Database

MySQL Database: registrar

Source SQL file: others/registrar.sql

Tables:

course (id, code, title)

section (course_id, title, amount)

enroll (section_id, student, action_date)

Page 8: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

8

Activity 3: Play with Redis

To run Redis server.

>> redis-server

To run Redis CLI.

>> redis-cli

cli >> set x 10

cli >> get x

Structure Type

- STRING

- LIST

- SET

- HASH

- ZSET

SETGET

HMSETHGETALLHINCRBY

Page 9: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

9

Activity 4: Caching

Source code: init.js

To run >> node init.js

To stop: Ctrl+C

Redis key:

● course:$course_code to keep course JSON

● enroll:$course_code to keep enroll_amount of all sections in the same course

Page 10: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

10

Activity 5: SPA & Socket.io

Node server source: server.js

To run Node server >> node server.js

Web client source: public/index.html, public/js/app.js

● Part 1: Search

● Part 2: Plan (involve Socket.io)

Page 11: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

11

Activity 6: SUB/PUB & Socket.io

Node server source: server.js

● Every Socket.io connection has it own relationship with Redis SUB/PUB channel.

● When web client register for a course, node.js subscribe for Redis SUB/PUB channel accordingly.

● Once a message in subscribed channel is published, node.js emit socket.io message to web client

Page 12: Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY 1 Aj. Suthon, Nong Gun, Nong Pop.

12

Activity 7: REST with PHP

PHP Server source: php/index.php

Web client source: public/index.html, public/js/app.js

Some modifications are required

- Apache configuration (related to document root)

- PHP configuration (related to PHP redis extensions)

● Part 1: Login

● Part 2: Enroll (Increase enroll amount and publish update message to Redis SUB/PUB channel)