Docs For developers Cloud data
Cloud data
Save and share values through Scrawlee, and connect the people using your Scrawl at the same time, with one file and a small package.
What can cloud data do?
Cloud data keeps values on Scrawlee for your Scrawl: values everyone shares (a counter, a board), each person's own values that only they read (settings, progress), and public values others can see (a best result, a status). Rooms connect the people using your Scrawl at the same moment, with typed messages and a shared state.
It takes two pieces: a scrawlee.yml file that declares every value with its type and rules, and the scrawlee package your code calls. Scrawlee checks every write against the file, so a modified copy of your code cannot store anything the file does not allow.
People decide what your Scrawl gets. The first time it saves, Scrawlee asks them which name others see (their username, a random name or none) and whether your Scrawl may save. Your code never gets their account or their email.
Add cloud data to a Scrawl
- Install the package with
npm install scrawlee, or without a bundler import it fromhttps://www.scrawlee.com/sdk/v1.jsin a<script type="module">. - Write
scrawlee.ymlat the root of what you publish (inpublic/with Vite and most frameworks). - Call the package in your code, try it on your development server, then publish.
# yaml-language-server: $schema=https://www.scrawlee.com/schema/scrawlee.yml.json
cloud:
data:
visits: { scope: shared, type: int, default: 0, write: add, step_max: 1, rate: 10/min }
best: { scope: public, type: int, min: 0, write: increase, rank: desc }
prefs:
scope: own
type: object
fields:
theme: { type: enum, values: [light, dark] } Read, write and follow a value
set and add resolve once Scrawlee stored the value, and reject with a CloudError when a rule refuses it. watch calls you with the value now and again on every change anyone makes, as Scrawlee stored it. A map is read and written by key.
import { cloud } from 'scrawlee'
const visits = cloud.data('visits')
await visits.add(1)
visits.watch((n) => (counter.textContent = n))
const best = cloud.data('best')
try {
await best.set(1200)
} catch (e) {
if (e.code === 'rule') console.log('not higher than before')
}
const { entries, me } = await best.ranking({ limit: 10 }) When does the consent panel open?
The first write opens it by itself, inside your Scrawl's area. To open it at a moment you choose, a button or a step of your Scrawl, call await cloud.ask(). Nothing opens when your Scrawl loads.
If the person chose read only or nothing, writes reject with no_access. Signed out, they reject with not_signed_in. cloud.on('access', (me) => ...) tells you when they change their choices from the cloud button.
With sign_in: required in scrawlee.yml, the Scrawl page asks for sign-in and consent before your Scrawl loads. Use it only if your Scrawl cannot work otherwise.
Connect people at the same time
join() puts the person in a room of that kind with space left. join(code) opens the room a code names, so people who share the code end up together. Messages are declared in scrawlee.yml with their type and rate, and are sent without waiting for an answer: a refused one arrives on room.on('error').
The room state follows the same rules as values. After a lost connection the package reconnects and joins again, and room.on('reset') tells you to redraw from room.members and room.state.
const room = await cloud.room('match').join('friends-42')
room.on('join', (m) => addPlayer(m))
room.on('leave', (m) => removePlayer(m))
room.on('move', (data, from) => apply(data, from))
room.send('move', { x: 1, y: 2 })
await room.state.set('turn', 2)
room.state.watch('turn', (t) => showTurn(t)) Test on your development server
On localhost and other development addresses, the package runs a local Scrawlee in your browser. Values stay in the browser, each tab is another person (open two tabs to try a room), and the rules of your scrawlee.yml apply with Scrawlee's messages. A small panel marked Local test stands in for the consent panel.
The package reads scrawlee.yml next to your page or at the root of the development server. If the file has a problem, the browser console lists it as Scrawlee would at publish. cloud.resetLocalData() starts over, and cloud.configure({ mode: 'local' }) forces the local mode anywhere.
On Scrawlee, outside it, and elsewhere
On its Scrawlee page, your Scrawl has everything. Opened at its own scrawlee.me address, it can read shared and public values but not write: writes reject with read_only. Anywhere else, calls reject with unavailable. (await cloud.ready()).mode tells you which: scrawlee, outside, local or unavailable.