About
Binary Data
Overview
Convert String to ArrayBuffer
Convert ArrayBuffer to String
Convert ArrayBuffers to String
Handle Errors Converting ArrayBuffer to String
Immutable ArrayBuffers
Resize an ArrayBuffer
Combine ArrayBuffers
Convert Base64 to Binary Data
Convert Binary Data to Base64
Convert Binary Data to Hex
Convert Hex to Binary Data
Calculate CRC for Binary Data
Compress Binary Data – One Buffer
Compress Binary Data – Streaming
Decompress Binary Data – One Buffer
Decompress Binary Data – Streaming
DNS
Overview
Resolve Name
Resolve Multiple Names
DNS Service Discovery
Overview
Claim Local Name
Advertise Services
Discover Services
EventSource
Overview
Connect
Connect Securely
Close
Receive
Connection Information
Files
Overview
Create, Open, and Close File
Read File
Write File
Delete File
Get File Information
Create Directory
Enumerate Directory
Delete Directory
Open Directory
HTTP
Overview
Make Request using fetch()
Make Secure Request using fetch()
Send Request Headers using fetch()
Receive Response Headers using fetch()
Send Request Body using fetch()
Make Request using HTTP Client
Make Secure Request using HTTP Client
Send Request Headers using HTTP Client
Receive Response Headers using HTTP Client
Send Request Body using HTTP Client
Implementing ECMA-419 Modules
Overview
Constructor Sets `target`
Constructor IO
Constructor Clean-up on Failure
`close()` and `[Symbol.dispose]`
Calling Callbacks
Setting Options with `configure()`
Keep Instance Surface Clean
Key-Value Storage
Overview
Read and Write Values using Web Storage
Delete Keys using Web Storage
Enumerate Keys using Web Storage
Read and Write Values using Key-Value Pair
Change Data Formats using Key-Value Pair
Delete Keys using Key-Value Pair
Enumerate Keys using Key-Value Pair
Logging
Overview
Logging with Console
Logging with trace()
MQTT
Overview
Connect to MQTT Server using MQTT()
Connect Securely to MQTT Server using MQTT()
Close Connection using MQTT()
Publish Message using MQTT()
Subscribe to Topic using MQTT()
Receive Messages using MQTT()
Get Connection Information using MQTT()
Connect to MQTT Server using MQTT Client
Connect Securely to MQTT Server using MQTT Client
Close Connection using MQTT Client
Publish Message using MQTT Client
Subscribe to Topic using MQTT Client
Receive Messages using MQTT Client
Optimizing Embedded JavaScript
Overview
When to Optimize
Know Where to Optimize
Loop through an Array
Iterate Over a String
Build a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Append to an Array
Operate on Bits
Define Class Methods
Streams
Time
Overview
Get Unix Time
Get Time of Day
Get Date
Get Time Since System Start
Get Microseconds
Set System Date and Time
Get Time and Date from Real-Time Clock
Set Real-Time Clock Time
Get Time and Date from Network
Sleep
Time Callbacks
Overview
One-Time Callback
Repeating Callback
Repeating Callback with Initial Delay
Immediate Callback
Reschedule Callback
Cancel Callback
Suspend Callback
Transport Layer Security (TLS)
Overview
Include Public Certificates
Include Private Certificates
Diagnostics
DER and PEM Certificates
WebSocket
Overview
Connect to Server using WebSocket()
Connect Securely to Server using WebSocket()
Close Connection using WebSocket()
Send Message using WebSocket()
Receive Message using WebSocket()
Get Connection Information using WebSocket()
Connect to Server using WebSocket Client
Connect Securely to Server using WebSocket Client
Close Connection using WebSocket Client
Send Message using WebSocket Client
Receive Message using WebSocket Client
Control Messages using WebSocket Client
Connect to Server using WebSocketStream
Connect Securely to Server using WebSocketStream
Close Connection using WebSocketStream
Send Message using WebSocketStream
Receive Message using WebSocketStream
Wi-Fi
Overview
Scan for Access Points
Scan Continuously for Access Points
Connect
Reconnect Automatically
Disconnect
Get Connection Information
Use Static IP Address
Key-value storage lets a project store small pieces of data, such as settings and state, that persist across restarts. The Moddable SDK provides two APIs: the high-level web standard Web Storage API (localStorage), and the low-level ECMA-419 embedded standard Key-Value store. Both organize data into named domains, each holding a set of key-value pairs. In fact, Web Storage is implemented using the Key-Value store.
Key-Value store replaces the Moddable SDK's Preferences module. The names are quite different, so this is easy to miss.
Using ECMA-419 Key-Value Store
If Web Storage works for your project, you should use it. Because localStorage is a well-known web standard, many developers are already familiar with it, and it requires the least code. Web Storage values are always strings, so binary and structured data must be encoded, for example as Base64 or JSON.
The Key-Value store is a better choice when you work directly with binary data. It reads and writes Byte Buffers by default and supports several numeric and string data formats. It also has lower runtime overhead and lets you iterate a domain's keys directly.
To access key-value pairs using Web Storage, a Web Storage instance must first be bound to a Key-Value store domain. Because localStorage is commonly used on the Web, this example binds the domain "local" to the global variable localStorage. You can create several instances of Web Storage, for example, a sessionStorage for Web compatibility.
import WebStorage from "webstorage";
const kvp = device.keyValue.open({
path: "local"
});
globalThis.localStorage = new WebStorage(kvp);
When building with mcpack or developing for Alloy on Pebble OS, this step is unnecessary as both create localStorage for you.
Include the Web Storage manifest in your project's manifest.json. It includes the Key-Value store, so no other manifest is needed:
$(MODDABLE)/examples/io/storage/webstorage/manifest_webstorage.json
Then, import the module in your JavaScript source code:
import WebStorage from "webstorage";
To use the Key-Value store directly, include its manifest:
$(MODDABLE)/modules/io/storage/manifest.json
Then, import the module in your JavaScript source code:
import keyValue from "embedded:storage/key-value";
When building with mcpack, the Web Storage manifest is included automatically when your project uses the localStorage global. If your project uses webstorage with an import statement, it's manifest is automatically included.
To use the Key-Value store directly, import the module in your JavaScript source code. mcpack automatically includes the manifest.
import keyValue from "embedded:storage/key-value";
- Documentation
- Standards
- Example
- Implementations
- TypeScript Declarations