Persistence & State. SWE 432, Fall 2016 Design and Implementation of Software for the Web

Similar documents
Persistence. SWE 432, Fall 2017 Design and Implementation of Software for the Web

NoSQL & Firebase. SWE 432, Fall Web Application Development

Tools. SWE 432, Fall Design and Implementation of Software for the Web

Backend Development. SWE 432, Fall Web Application Development

Backend Development. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Security. SWE 432, Fall 2017 Design and Implementation of Software for the Web

We are assuming you have node installed!

Distributed Architectures & Microservices. CS 475, Spring 2018 Concurrent & Distributed Systems

Microservices. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Frontend Frameworks. SWE 432, Fall 2016 Design and Implementation of Software for the Web

Cookies, sessions and authentication

Sessions. Mendel Rosenblum. CS142 Lecture Notes - Sessions

What is Node.js? Tim Davis Director, The Turtle Partnership Ltd

Lecture 9a: Sessions and Cookies

Tuesday, January 13, Backend III: Node.js with Databases

Real Life Web Development. Joseph Paul Cohen

CMSC 332 Computer Networking Web and FTP

Large-Scale Web Applications

Web, HTTP and Web Caching

Client Side JavaScript and AJAX

Human-Computer Interaction Design

Computer Networks. Wenzhong Li. Nanjing University

Node.js. Node.js Overview. CS144: Web Applications

IERG Tutuorial 5. Benedict Mak

Human-Computer Interaction Design

1-1. Switching Networks (Fall 2010) EE 586 Communication and. September Lecture 10

RESTful APIs ECS 189 WEB PROGRAMMING. Browser s view. Browser s view. Browser s view. Browser s view. Which will It be for photobooth?

C exam. Number: C Passing Score: 800 Time Limit: 120 min IBM C IBM Cloud Platform Application Development

File systems, databases, cloud storage

Security and Privacy. SWE 432, Fall 2016 Design and Implementation of Software for the Web

Unifer Documentation. Release V1.0. Matthew S

Lecture 7b: HTTP. Feb. 24, Internet and Intranet Protocols and Applications

INTERNET ENGINEERING. HTTP Protocol. Sadegh Aliakbary

Scaling DreamFactory

Tutorial 4 Data Persistence in Java

Topic 15: Authentication

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

Managing State. Chapter 13

HyperText Transfer Protocol

Back-end architecture

Express.JS. Prof. Cesare Pautasso Modularity

CS193X: Web Programming Fundamentals

Review of Previous Lecture

Introduction. Server-side Techniques. Introduction. 2 modes in the PHP processor:

CSC 4900 Computer Networks:

Cloud platforms T Mobile Systems Programming

BlueMix Hands-On Workshop Lab A - Building and Deploying BlueMix Applications

Real World Web Scalability. Ask Bjørn Hansen Develooper LLC

Best Practices. For developing a web game in modern browsers. Colt "MainRoach" McAnlis

Module 6 Node.js and Socket.IO

Microservices with Node.js

Standard 1 The student will author web pages using the HyperText Markup Language (HTML)

COMP6218: Content Caches. Prof Leslie Carr

CSCE 120: Learning To Code


Session 8. Reading and Reference. en.wikipedia.org/wiki/list_of_http_headers. en.wikipedia.org/wiki/http_status_codes

From the Beginning: Your First Node.js Web Service

HTTP Reading: Section and COS 461: Computer Networks Spring 2013

Quick.JS Documentation

Web Application Development

WEB SECURITY WORKSHOP TEXSAW Presented by Solomon Boyd and Jiayang Wang

JSN PageBuilder 2 User Manual

Announcements. PS 3 is out (see the usual place on the course web) Be sure to read my notes carefully Also read. Take a break around 10:15am

More on Testing and Large Scale Web Apps

Cloud platforms. T Mobile Systems Programming

I'm Andy Glover and this is the Java Technical Series of. the developerworks podcasts. My guest is Brian Jakovich. He is the

CS 43: Computer Networks. HTTP September 10, 2018

8.0 Help for Community Managers Release Notes System Requirements Administering Jive for Office... 6

big picture parallel db (one data center) mix of OLTP and batch analysis lots of data, high r/w rates, 1000s of cheap boxes thus many failures

Application Layer Introduction; HTTP; FTP

Serverless Single Page Web Apps, Part Four. CSCI 5828: Foundations of Software Engineering Lecture 24 11/10/2016

CS50 Quiz Review. November 13, 2017

GET /index.php HTTP/1.1 Host: User- agent: Mozilla/4.0

Cloud Computing with APL. Morten Kromberg, CXO, Dyalog

Deployment. Chris Wilson, AfNOG / 26

(Refer Slide Time: 01:40)

Outline. ASP 2012 Grid School

Cloud Help for Community Managers...3. Release Notes System Requirements Administering Jive for Office... 6

CSE 333 Lecture HTTP

DATABASE SYSTEMS. Database programming in a web environment. Database System Course,

COMP 2406: Fundamentals of Web Applications. Fall 2013 Mid-Term Exam Solutions

EE 122: HyperText Transfer Protocol (HTTP)

Microservices without the Servers: AWS Lambda in Action

Lecture 6 Application Layer. Antonio Cianfrani DIET Department Networking Group netlab.uniroma1.it

Application Protocols and HTTP

The Old World. Have you ever had to collaborate on a project by

Using and Developing with Azure. Joshua Drew

NODE.JS SERVER SIDE JAVASCRIPT. Introduc)on Node.js

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

You are Unable to Log into MBEF

CS144: Sessions. Cookie : CS144: Web Applications

MongoDB. Robert M. Vunabandi

Full Stack boot camp

Extra Notes - Data Stores & APIs - using MongoDB and native driver

CSC116: Introduction to Computing - Java

Chapter 2: Application Layer. Chapter 2 Application Layer. Some network apps. Application architectures. Chapter 2: Application layer

CSc 337 LECTURE 16: WRITING YOUR OWN WEB SERVICE

Future Web App Technologies

Security. CSC309 TA: Sukwon Oh

CS4/MSc Computer Networking. Lecture 3: The Application Layer

Transcription:

Persistence & State SWE 432, Fall 2016 Design and Implementation of Software for the Web

Today What s state for our web apps? How do we store it, where do we store it, and why there? For further reading: http://www.w3schools.com/html/html5_webstorage.asp https://github.com/gmu-swe432/lecture15demos https://www.npmjs.com/package/google-cloud https://devcenter.heroku.com/articles/getting-started-with-nodejs 2

What s State in our web app?

Web App State Application state includes all of our data (not code) What kinds of data are we concerned about? What user is logged in? What interactions have they had with us before? What data have they given us? What data have others given us? Where do we store all of these things? 4

State: Example Amazon.com Home page Login Browse Add to cart Still logged in Still logged in LaToza/Bell GMU SWE 432 Fall 2016 visit amazon.com Still logged in, still have cart 5

Where do we save stuff? Many options of where we keep our data Where do we want to put it? How do we get it to where it needs to be? Goals: Cost Efficiency Stability Web Front End Our Node Backend Firebase Other storage 6

Where do we save stuff? Probably depends on how often we need to show it to the user, and how permanently we need to store it Examples: What user is logged in? (Transient, relevant to user and backend) What s in my shopping cart? (Semi-transient, relevant to user and backend) What products am I looking at? (Transient, relevant to user) What are all of the products (Long-term, parts are relevant to users) Web Front End Our Node Backend Firebase Other storage 7

Where do we save stuff? On client Data we might need to show again soon Fairly small (KB s or few MBs, not 100 MB s or GB s) Data we don t care about going away or being maliciously manipulated In memory on backend Data that we are working with that will fit in memory (MB s probably not GB s) Transient data that can disappear if the server crashes Cache or index of data stored externally On backend disk, database, or storage service(e.g., Firebase) Data we need persisted permanently Even if we ll be accessing it a lot, maybe we ll cache it somewhere so OK to pay performance penalty 8

Client Side State Original form of client state: Cookies Motivation: We want to correlate multiple requests But HTTP is stateless 9

Cookies String associated with a name/domain/path, stored at the browser Series of name-value pairs, interpreted by the web application Create in HTTP response with Set-Cookie: In all subsequent requests to this site, until cookie s expiration, the client sends the HTTP header Cookie: Often have an expiration (otherwise expire when browser closed) Various technical, privacy and security issues Inconsistent state after using back button, third-party cookies, cross-site scripting, 10

Maintaining Client Side State Web Front End Back End HTTP Request HTTP GET http://api.wunderground.com/api/ 3bee87321900cf14/conditions/q/VA/Fairfax.json How do we track request-response pairs? HTTP Response HTTP/1.1 200 OK : Apache/2.2.15 (CentOS) Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true X-CreationTime: 0.134 Last-Modified: Mon, 19 Sep 2016 17:37:52 GMT Content-Type: application/json; charset=utf-8 Expires: Mon, 19 Sep 2016 17:38:42 GMT Cache-Control: max-age=0, no-cache Pragma: no-cache Date: Mon, 19 Sep 2016 17:38:42 GMT Content-Length: 2589 Connection: keep-alive { "response": { "version":"0.1", "termsofservice":"http://www.wunderground.com/weather/api/d/terms.html", 11

Cookies and Requests Web Front End HTTP Request HTTP Response HTTP Request HTTP Response HTTP Request HTTP Response HTTP/1.1 200 OK... Set-Cookie: class=swe432... GET / HTTP/1.1... Cookie: class=swe432... GET / HTTP/1.1... Cookie: class=swe432... Back End 12

Cookies & NodeJS Use the cookieparser module Stateful Hello World: var express = require('express'); var cookieparser = require('cookie-parser'); var app = express(); var port = process.env.port 3000; app.use(cookieparser()); app.get('/', function (req, res) { if(req.cookies.hellosent == "true") res.send("i already said hello to you!"); else res.cookie("hellosent","true").send('hello World!'); }); app.listen(port, function () { console.log('example app listening on port' + port); }); Can see cookies in Chrome under Privacy 13

Cookies Demo https://github.com/gmu-swe432/lecture15demos/ tree/master/cookieshello 14

More complex state on frontend The most cookies you can have: 4KB (TOTAL per DOMAIN) Old solution: Cookie is a key to some data stored on server When client makes a request, server always includes this extra data being stored on server What s wrong with this old solution? Really slow - have to repetitively pass this same data back and forth 15

LocalStorage Hooray, HTML5: localstorage (Sticks around forever) sessionstorage (Sticks around until tab is closed) And two functions: setitem( key","value"); getitem( key ); var id = localstorage.getitem( userid ); Can store any string All pages in the same domain see the same localstorage and sessionstorage Alternatively: SQLite (SQL DB) that you can use in JS 16

Demo: LocalStorage https://github.com/gmu-swe432/lecture15demos/tree/master/ localstoragetodos 17

Keeping State on the Backend

Node and State Remember what a node route listener looks like app.get('/', function (req, res) { res.send('hello World!'); }); Each time a request comes in, a new callback runs How do we keep track of things? Well 19

Recall: Node Architecture Each new request goes to a new request handler New Request Node App Express Request Handler New Request New Request Request Handler Keep state here! Request Handler New Request Request Handler New Request Request Handler While the server is running though, it s all one app handling all requests 20

Keeping State in Node Global variables var express = require('express'); var app = express(); var port = process.env.port 3000; var counter = 0; app.get('/', function (req, res) { res.send('hello World has been said ' + counter + ' times!'); counter++; }); app.listen(port, function () { console.log('example app listening on port' + port); }); Pros/cons? Keep data between requests Goes away when your server stops Should use for transient state or as cache 21

Demo: Statefull hello https://github.com/gmu-swe432/lecture15demos/ tree/master/statefulhello 22

The Bigger Backend State Space Databases SQL: MySQL, PostgreSQL, SQL, NoSQL: Firebase, Mongo, Reference: RESTful todos Files Store arbitrary files on disk JSON Pictures, etc Even better: blob stores 23

How do we store our files? Dealing with text is easy - we already figured out firebase Could use other databases too but that s another class! But What about pictures? What about movies? What about big huge text files? Aka Binary Large OBject (BLOB) Collection of binary data stored as a single entity Generic terms for an entity that is array of byte 24

Blobs: Storing uploaded files Example: User uploads picture and then? somehow process the file? 25

Working with Blobs Module: express-fileupload Long story... can't use body-parser when you are taking files Simplest case: take a file, save it on the server app.post('/upload', function(req, res) { var samplefile; samplefile = req.files.samplefile; samplefile.mv('/somewhere/on/your/server/filename.jpg', function(err) { if (err) { res.status(500).send(err); } else { res.send('file uploaded!'); } }); }); 26

Where to store blobs Saving them on our server is fine, but What if we don't want to deal with making sure we have enough storage What if we don't want to deal with backing up those files What if our app has too many requests for one server and state needs to be shared between load-balanced servers What if we want someone else to deal with administering a server 27

Blob stores Amazon, Google, and others want to let you use their platform to solve this! Distributes file Client Client Uploads file Node Backend Client Client Client Google Cloud Client Client 28

Blob Stores Uploads file Client Node Backend Returns link Typical workflow: t uploads file to your backend end persists file to blob store kend saves link to file, e.g. i Google Cloud 29

Google Cloud Storage You get to store 5GB for free! Howto: https://www.npmjs.com/package/google-cloud Demo: Todos with images + Blobstore Uses Multer instead of express-fileupload Multer lets you temporarily store a file in memory as it goes directly to a remote server (rather than save it to your server first) https://github.com/gmu-swe432/ lecture15demos/tree/master/blobstore 30

Where do we run these backends? So, running this on your laptop is not great Who wants to run their own actual server? Solution: App hosting providers Example: Heroku Big infrastructure companies that will deal with the annoying stuff for you https://devcenter.heroku.com/articles/gettingstarted-with-nodejs 31

Heroku Once you install Heroku, you communicate via git Instead of just pushing to GitHub, push to Heroku Then Heroku does some magic Do NOT use GHPages + Heroku unless you want extra pain: just run your app on Heroku (including frontend) Your computer GitHub Git Node App Heroku Git Heroku Deployment s 32

Heroku Example 1: Create account, install Heroku on your machine 2: In our app directory, create file Procfile with following contents: web: node app.js 3: Type heroku create and follow instructions 5: Visit your app at the site listed in the result of the push (e.g. https://saltydepths-97600.herokuapp.com) Tells Heroku what to do when it gets your app 4: git push heroku master Deploys your code 33

Coming back to the high level Web Front End Our Node Backend Firebase Other storage Cookies LocalStorage In Memory Storage Maybe some files? Databases Blob stores Short-lived data In-between? Long-lived data 34

Exit-Ticket Activity Go to socrative.com and select Student Login Class: SWE432001 (Prof LaToza) or SWE432002 (Prof Bell) ID is your @gmu.edu email 1: How well did you understand today's material 2: What did you learn in today's class? For question 3: What state does your project have? You may not submit this activity if you are not present in lecture. Doing so will be considered academic dishonesty.