Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF...

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

Transcript of Real-time Web Application with Node.js CENTRE FOR NETWORK RESEARCH COMPUTER ENGINEERING, PRINCE OF...

1

Real-time Web Application with Node.js

CENTRE FOR NETWORK RESEARCH

COMPUTER ENGINEERING, PRINCE OF SONGKLA UNIVERSITY

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

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)

4

CACHING

Read data Course data

struct type STRING

Struct type HASHEnroll data

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

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)

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)

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

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

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)

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

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)