The app-changing magic (and joy) of Web APIs

A presentation at HalfStack Charlotte in April 2023 in Charlotte, NC, USA by Trent Willis

Slide 1

Slide 1

Web APIs The app-changing magic (and joy) of

Slide 2

Slide 2

Raise your ✋ if you’ve built a website/app without any libraries in the last year or so

Slide 3

Slide 3

Developing with libraries is so common, many of us can’t remember what it is like to develop without them

Slide 4

Slide 4

This isn’t a bad thing

Slide 5

Slide 5

What is bad, is many of us have forgotten about the web platform

Slide 6

Slide 6

Web APIs The app-changing magic (and joy) of

Slide 7

Slide 7

Trent Willis Staff Software Engineer

Slide 8

Slide 8

What is a Web API? A programming interface built in to the browser But, maybe not all browsers Usually come from the Web Hypertext Application Technology Working Group (WHATWG) Standards

Slide 9

Slide 9

Standards help you discover new realms of web development

Slide 10

Slide 10

Web APIs help you discover new realms of web development

Slide 11

Slide 11

The MessageChannel

Slide 12

Slide 12

Execution Context Execution Context = Website, Iframe, Web/Shared Worker

Slide 13

Slide 13

Execution Context Message Channel Execution Context 2 Execution Context = Website, Iframe, Web/Shared Worker

Slide 14

Slide 14

Why would you need that? Get things off the main thread (and stay off)

Slide 15

Slide 15

Website

Slide 16

Slide 16

Website Web Worker

Slide 17

Slide 17

Website Web Worker Web Worker 2

Slide 18

Slide 18

Website Web Worker Web Worker 2

Slide 19

Slide 19

Website Web Worker Message Channel Web Worker 2

Slide 20

Slide 20

Why would you need that? Get things off the main thread (and stay off) Decouple execution contexts

Slide 21

Slide 21

Website

Slide 22

Slide 22

Website Sandboxed Iframe

Slide 23

Slide 23

Main Thread Website Sandboxed Iframe

Slide 24

Slide 24

Main Thread Worker Thread Website Sandboxed Iframe Web Worker

Slide 25

Slide 25

Main Thread Worker Thread Website Sandboxed Iframe Web Worker

Slide 26

Slide 26

Main Thread Worker Thread Website Sandboxed Iframe Web Worker

Slide 27

Slide 27

Sandybox Secure, non-blocking sandbox for arbitrary (i.e., user created) functions github.com/trentmwillis/sandybox

Slide 28

Slide 28

The real APIs are the things we learned along the way MessagePort Web Locks Iframe Shadow Realms* Web Worker Compartments** Shared Worker HTML Sanitizer*** Broadcast Channel

Slide 29

Slide 29

The real APIs are the things we learned along the way Transferable Knowledge Multi-Threading Execution Contexts

Slide 30

Slide 30

Web APIs help you learn Transferable programming skills

Slide 31

Slide 31

The FetchEvent.respondWith()

Slide 32

Slide 32

Website

Slide 33

Slide 33

Website Service Worker

Slide 34

Slide 34

Website Service Worker Server

Slide 35

Slide 35

Website Service Worker Server

Slide 36

Slide 36

Service Worker Website event.respondWith(stuff) Cache Server

Slide 37

Slide 37

Why would you need that? Caching network responses Invisible file/data manipulation Polyfilling/creative new file types Mocking non-XHR responses

Slide 38

Slide 38

Service Worker Website Server

Slide 39

Slide 39

Blackbox Service Worker Website Server

Slide 40

Slide 40

Blackbox GET image.epng* Service Worker Website .epng .epng event.respondWith(stuff) .epng .png Transform† *.epng = custom encrypted image format not supported by browsers †in this case, decrypt .epng to .png Server

Slide 41

Slide 41

Blackbox GET script.ts Service Worker Website .ts .ts event.respondWith(stuff) .ts .js Transform† †in this case, compile .ts to .js Server

Slide 42

Slide 42

TypeScript in the Browser Use <script src=“index.ts”></script> typescript-in-browser.glitch.me

Slide 43

Slide 43

Client-Side TypeScript Plugin System Only need to store the TypeScript file No server-side compilation But! breaks “progressive enhancement”

Slide 44

Slide 44

The real APIs are the things we learned along the way “Advanced” Fetch Web Crypto Response (De)Compression* Streams

Slide 45

Slide 45

The real APIs are the things we learned along the way Network request and response handling Caching Streams File Formats Graceful Degradation Creative API Usage

Slide 46

Slide 46

Web APIs help you come up with new Creative ideas

Slide 47

Slide 47

The AudioWorkletProcessor

Slide 48

Slide 48

Website

Slide 49

Slide 49

Website AudioContext

Slide 50

Slide 50

Website AudioContext AudioWorkletNode

Slide 51

Slide 51

Website Main Thread Web Audio Rendering Thread AudioContext AudioWorkletNode AudioWorkletProcessor

Slide 52

Slide 52

Website Main Thread AudioContext AudioWorkletNode AudioWorkletProcessor Web Audio Rendering Thread process()

Slide 53

Slide 53

Website Main Thread AudioContext AudioWorkletNode AudioWorkletProcessor Web Audio Rendering Thread process() Audio Source

Slide 54

Slide 54

Website Main Thread AudioContext AudioWorkletNode Audio Output (Speakers) AudioWorkletProcessor Web Audio Rendering Thread process() Audio Source

Slide 55

Slide 55

Why would you need that? Custom signal processing (effects) Off-main thread audio visualization Custom sound/noise generation

Slide 56

Slide 56

Website Main Thread AudioContext AudioWorkletNode Audio Output (Speakers) AudioWorkletProcessor Web Audio Rendering Thread process() Audio Source

Slide 57

Slide 57

Website Main Thread AudioContext AudioWorkletNode AudioWorkletProcessor Web Audio Rendering Thread process() Audio Output (Speakers)

Slide 58

Slide 58

Website Main Thread AudioContext AudioWorkletNode AudioWorkletProcessor Web Audio Rendering Thread process() RNG(-1, 1) (White Noise) Audio Output (Speakers)

Slide 59

Slide 59

DeepBreathing.app “Smooth” brown noise generation

Slide 60

Slide 60

Website Main Thread AudioContext AudioWorkletNode AudioWorkletProcessor Web Audio Rendering Thread process() RNG(-1, 1) (White Noise) Audio Output (Speakers)

Slide 61

Slide 61

AudioContext AudioWorkletNode White Noise From AudioWorkletProcessor

Slide 62

Slide 62

AudioContext AudioWorkletNode BiquadFilterNode (Lowpass) White Noise From AudioWorkletProcessor

Slide 63

Slide 63

AudioContext AudioWorkletNode BiquadFilterNode (Lowpass) White Noise From AudioWorkletProcessor BiquadFilterNode (Highpass)

Slide 64

Slide 64

AudioContext AudioWorkletNode BiquadFilterNode (Lowpass) White Noise From AudioWorkletProcessor BiquadFilterNode (Highpass) GainNode (Volume)

Slide 65

Slide 65

AudioContext White Noise From AudioWorkletProcessor BiquadFilterNode (Highpass) GainNode (Volume) Brown Noise To Speakers AudioWorkletNode BiquadFilterNode (Lowpass)

Slide 66

Slide 66

AudioContext OscillatorNode (LFO) White Noise From AudioWorkletProcessor BiquadFilterNode (Highpass) GainNode (Volume) Brown Noise To Speakers AudioWorkletNode BiquadFilterNode (Lowpass)

Slide 67

Slide 67

AudioContext GainNode (Intensity) OscillatorNode (LFO) White Noise From AudioWorkletProcessor BiquadFilterNode (Highpass) GainNode (Volume) Brown Noise To Speakers AudioWorkletNode BiquadFilterNode (Lowpass)

Slide 68

Slide 68

AudioContext Frequency GainNode (Intensity) OscillatorNode (LFO) White Noise From AudioWorkletProcessor BiquadFilterNode (Highpass) GainNode (Volume) Brown Noise To Speakers AudioWorkletNode BiquadFilterNode (Lowpass)

Slide 69

Slide 69

DeepBreathing.app “Smooth” brown noise generation Immersive experience beyond visuals Cross-browser (Safari) debugging

Slide 70

Slide 70

The real APIs are the things we learned along the way Web Audio APIs Permissions Web MIDI Vibration Media Capture and Streams

Slide 71

Slide 71

The real APIs are the things we learned along the way Audio Concepts Input Devices Signal Processing Streams, Threads, File Formats, etc.

Slide 72

Slide 72

Cool, so what?

Slide 73

Slide 73

Web APIs can unlock new architectures

Slide 74

Slide 74

Web APIs can unlock new creative power

Slide 75

Slide 75

Web APIs can unlock new perspectives

Slide 76

Slide 76

Web APIs can unlock new perspectives Why would I need that? What else is related to this? How does this improve my understanding of the web? What transferrable skills am I learning? When is the next JS trivia night?

Slide 77

Slide 77

Web APIs can restore the joy of web development. What are you going to build?