Client-side Web APIs

Repo: https://github.com/Yonet/client-side-apis

Created by Ayşegül Yönet - @AysSomething

About me

Web APIs

  • What are APIs?
  • How do APIs work?
  • How do we debug APIs?

Web APIs

Application Programming Interfaces (APIs) are constructs made available in programming languages to allow developers to create complex functionality more easily. They abstract more complex code away from you, providing some easier syntax to use in its place.

  • Browser APIs
  • 3rd Party APIs

What are Browser APIs?

  • Animation
  • DOM
  • Canvas
  • WebGL
  • WebSocket

What are Third party APIs?

Smart Bulb

How do APIs work?

  • Objects
  • Recognizable Entry Points
  • Events to handle changes in state
  • Security mechanisms

Setup

Enable following experimental flags on chrome browser by typing chrome://flags on chrome.

  • Experimental JavaScript: #enable-javascript-harmony
  • Experimental Web Platform features: #enable-experimental-web-platform-features
  • Developer Tools experiments: #enable-javascript-harmony
  • Image Capture API: #enable-image-capture-api

Network Information


enum ConnectionType {
	"bluetooth",
	"cellular",
	"ethernet",
	"mixed",
	"none",
	"other",
	"unknown",
	"wifi",
	"wimax"
};

Network Information


enum EffectiveConnectionType {
	"2g",
	"3g",
	"4g",
	"slow-2g"
};

Network Information


interface NetworkInformation : EventTarget {
	readonly attribute ConnectionType          type;
	readonly attribute EffectiveConnectionType effectiveType;
	readonly attribute Megabit                 downlinkMax;
	readonly attribute Megabit                 downlink;
	readonly attribute Millisecond             rtt;
	readonly attribute boolean                 saveData;
	         attribute EventHandler            onchange;
};

The "saveData" attribute, when getting, returns true if the user has requested a reduced data usage mode from the user agent, and false otherwise.

Network Information


navigator.connection.addEventListener('change', changeHandler);

Bluetooth & Bluetooth Low Energy

  • Heart monitors
  • Fitbit
  • Sensors

Bluetooth

Enable Developer Mode

Bluetooth


navigator.bluetooth.requestDevice(options);
navigator.bluetooth.requestDevice({acceptAllDevices:true});
      

Bluetooth


navigator.bluetooth.requestDevice(options)
   .then(device => {
     console.log('Yay!I found your device: ' + device.name)
   });
    

Bluetooth


navigator.bluetooth.requestDevice(options)
    .then(device => {
        device.gatt.connect())
    };
      

Push API

Hardware

Device Orientation

screen-object

Geolocation API

  • Geolocation
  • Position
  • Coordinates

Geolocation

  • Geolocation.getCurrentPosition(success, error?, [options]):
    • enableHighAccuracy = booleanValue
    • timeout (milliseconds)
    • maximumAge (milliseconds)
  • Geolocation.watchPosition()
  • Geolocation.clearWatch()

Geolocation


const options = {
   enableHighAccuracy: false,
   timeout: 5000,
   maximumAge: 0
};

functions success(position) {};
function error(err) {};

const watchId = navigator.geolocation.watchPosition(success, error, options);
navigator.geolocation.clearWatch(watchId);
      

Camera

Data Management APIs

  • Persistent Storage
  • Temporary Storage
  • Past:Cookies
  • Current: Web Storage and IndexedDB
  • Future: Cache API

Web Storage API

  • Session Storage
  • Local Storage

        localStorage.setItem('name','Aysegul');
        var myName = localStorage.getItem('name');
        localStorage.removeItem('name');
      
Open Chrome Dev Tools > Application > Storage > Local Storage

Session Storage

Cleared when the user session is over


// Save data to sessionStorage
sessionStorage.setItem('key', 'value');

// Get saved data from sessionStorage
var data = sessionStorage.getItem('key');

// Remove saved data from sessionStorage
sessionStorage.removeItem('key');

// Remove all saved data from sessionStorage
sessionStorage.clear();

Local Storage

  • Limited to ~10 MB per origin
  • Cleared upon exiting on Incognito mode
  • Since localStorage and sessionStorage are instances, they do not support prototyping. However, Storage API supports prototyping.

IndexedDB

  • Good for large amounts of structured data that requires high performance retrieval
  • Operations as done asynchronously as opposed to the synchronous Storage API.
  • Storage limit is browser and client dependent.
  • You can create indexes based on multiple properties of objects resulting in fast query times.
  • Intended to power offline webapps.

IndexedDB

  • dexie.js: A wrapper for IndexedDB that allows much faster code development via nice, simple syntax.
  • ZangoDB: A MongoDB-like interface for IndexedDB that supports most of the familiar filtering, projection, sorting, updating and aggregation features of MongoDB.
  • JsStore: An IndexedDB wrapper with SQL like syntax.

Resources

THE END

- @AysSomething
- Demo source code