···
3
+
In this guide, we're going to build a **simple multi-user app** that publishes your current "status" as an emoji.
5
+
At various points we will cover how to:
8
+
- Fetch information about users (profiles)
9
+
- Listen to the network firehose for new data
10
+
- Publish data on the user's account using a custom schema
12
+
We're going to keep this light so you can quickly wrap your head around ATProto. There will be links with more information about each step.
14
+
## Where are we going?
16
+
Data in the Atmosphere is stored on users' personal repos. It's almost like each user has their own website. Our goal is to aggregate data from the users into our SQLite DB.
18
+
Think of our app like a Google. If Google's job was to say which emoji each website had under `/status.json`, then it would show something like:
20
+
- `nytimes.com` is feeling 📰 according to `https://nytimes.com/status.json`
21
+
- `bsky.app` is feeling 🦋 according to `https://bsky.app/status.json`
22
+
- `reddit.com` is feeling 🤓 according to `https://reddit.com/status.json`
24
+
The Atmosphere works the same way, except we're going to check `at://` instead of `https://`. Each user has a data repo under an `at://` URL. We'll crawl all the `at://`s in the Atmosphere for all the `/status.json` records and aggregate them into our SQLite database.
26
+
## Step 1. Starting with our ExpressJS app
28
+
Start by cloning the repo and installing packages.
34
+
npm run dev # you can leave this running and it will auto-reload
37
+
Our repo is a regular Web app. We're rendering our HTML server-side like it's 1999. We also have a SQLite database that we're managing with [Kysley](#todo).
42
+
- NodeJS web server ([express](#todo))
43
+
- SQLite database ([Kysley](#todo))
44
+
- Server-side rendering ([uhtml](#todo))
46
+
With each step we'll explain how our Web app taps into the Atmosphere. Refer to the codebase for more detailed code — again, this tutorial is going to keep it light and quick to digest.
48
+
## Step 2. Signing in with OAuth
50
+
When somebody logs into our app, they'll give us read & write access to their personal `at://` repo. We'll use that to write the `status.json` record.
52
+
We're going to accomplish this using OAuth ([spec](#todo)). You can find a [more extensive OAuth guide here](#todo), but for now just know that most of the OAuth flows are going to be handled for us using the [@atproto/oauth-client-node](#todo) library. This is the arrangement we're aiming toward:
55
+
┌─App Server───────────────────┐
56
+
│ ┌─► Session store ◄┐ │
57
+
│ │ │ │ ┌───────────────┐
58
+
│ App code ──────►OAuth client─┼───►│ User's server │
59
+
└────▲─────────────────────────┘ └───────────────┘
65
+
When the user logs in, the OAuth client will create a new session with their repo server and give us read/write access along with basic user info.
67
+
Our login page just asks the user for their "handle," which is the domain name associated with their account. For [Bluesky](https://bsky.app) users, these tend to look like `alice.bsky.social`, but they can be any kind of domain (eg `alice.com`).
70
+
<!-- src/pages/login.ts -->
71
+
<form action="/login" method="post" class="login-form">
75
+
placeholder="Enter your handle (eg alice.bsky.social)"
78
+
<button type="submit">Log in</button>
82
+
When they submit the form, we tell our OAuth client to initiate the authorization flow and then redirect the user to their server to complete the process.
85
+
/** src/routes.ts **/
89
+
handler(async (req, res) => {
90
+
// Initiate the OAuth flow
91
+
const url = await oauthClient.authorize(handle)
92
+
return res.redirect(url.toString())
97
+
This is the same kind of SSO flow that Google or GitHub uses. The user will be asked for their password, then asked to confirm the session with your application.
99
+
When that finishes, they'll be sent back to `/oauth/callback` on our Web app. The OAuth client stores the access tokens for the server, and then we attach their account's [DID](#todo) to their cookie-session.
102
+
/** src/routes.ts **/
103
+
// OAuth callback to complete session creation
106
+
handler(async (req, res) => {
107
+
// Store the credentials
108
+
const { agent } = await oauthClient.callback(params)
110
+
// Attach the account DID to our user via a cookie
111
+
const session = await getIronSession(req, res)
112
+
session.did = agent.accountDid
113
+
await session.save()
115
+
// Send them back to the app
116
+
return res.redirect('/')
121
+
With that, we're in business! We now have a session with the user's `at://` repo server and can use that to access their data.
123
+
## Step 3. Fetching the user's profile
125
+
Why don't we learn something about our user? Let's start by getting the [Agent](#todo) object. The [Agent](#todo) is the client to the user's `at://` repo server.
128
+
/** src/routes.ts **/
129
+
async function getSessionAgent(
130
+
req: IncomingMessage,
131
+
res: ServerResponse<IncomingMessage>,
134
+
// Fetch the session from their cookie
135
+
const session = await getIronSession(req, res)
136
+
if (!session.did) return null
138
+
// "Restore" the agent for the user
139
+
return await ctx.oauthClient.restore(session.did).catch(async (err) => {
140
+
ctx.logger.warn({ err }, 'oauth restore failed')
141
+
await session.destroy()
147
+
Users publish JSON records on their `at://` repos. In [Bluesky](https://bsky.app), they publish a "profile" record which looks like this:
150
+
interface ProfileRecord {
151
+
displayName?: string // a human friendly name
152
+
description?: string // a short bio
153
+
avatar?: BlobRef // small profile picture
154
+
banner?: BlobRef // banner image to put on profiles
155
+
createdAt?: string // declared time this profile data was added
160
+
We're going to use the [Agent](#todo) to fetch this record to include in our app.
163
+
/** src/routes.ts **/
167
+
handler(async (req, res) => {
168
+
// If the user is signed in, get an agent which communicates with their server
169
+
const agent = await getSessionAgent(req, res, ctx)
172
+
// Serve the logged-out view
173
+
return res.type('html').send(page(home()))
176
+
// Fetch additional information about the logged-in user
177
+
const { data: profileRecord } = await agent.getRecord({
178
+
repo: agent.accountDid, // our user's repo
179
+
collection: 'app.bsky.actor.profile', // the bluesky profile record type
180
+
rkey: 'self', // the record's name
183
+
// Serve the logged-in view
186
+
.send(page(home({ profile: profileRecord.value || {} })))
191
+
With that data, we can give a nice personalized welcome banner for our user:
194
+
<!-- pages/home.ts -->
197
+
? html`<form action="/logout" method="post" class="session-form">
199
+
Hi, <strong>${profile.displayName || 'friend'}</strong>.
200
+
What's your status today?
203
+
<button type="submit">Log out</button>
206
+
: html`<div class="session-form">
207
+
<div><a href="/login">Log in</a> to set your status!</div>
209
+
<a href="/login" class="button">Log in</a>
215
+
## Step 4. Reading & writing records
217
+
You can think of the user repositories as collections of JSON records:
222
+
┌────────────┐ │ └────────┘
223
+
┌───| collection |◄─┤ ┌────────┐
224
+
┌──────┐ │ └────────────┘ └───| record │
225
+
│ repo |◄──┤ └────────┘
226
+
└──────┘ │ ┌────────────┐ ┌────────┐
227
+
└───┤ collection |◄─────| record │
228
+
└────────────┘ └────────┘
231
+
Let's look again at how we read the "profile" record:
234
+
await agent.getRecord({
235
+
repo: agent.accountDid, // The user
236
+
collection: 'app.bsky.actor.profile', // The collection
237
+
rkey: 'self', // The record name
241
+
We write records using a similar API. Since our goal is to write "status" records, let's look at how that will happen:
244
+
await agent.putRecord({
245
+
repo: agent.accountDid, // The user
246
+
collection: 'com.example.status', // The collection
247
+
rkey: 'self', // The record name
248
+
record: { // The record value
250
+
updatedAt: new Date().toISOString()
255
+
Our `POST /status` route is going to use this API to publish the user's status to their repo.
258
+
/** src/routes.ts **/
259
+
// "Set status" handler
262
+
handler(async (req, res) => {
263
+
// If the user is signed in, get an agent which communicates with their server
264
+
const agent = await getSessionAgent(req, res, ctx)
266
+
return res.status(401).json({ error: 'Session required' })
269
+
// Construct their status record
271
+
$type: 'com.example.status',
272
+
status: req.body?.status,
273
+
updatedAt: new Date().toISOString(),
277
+
// Write the status record to the user's repository
278
+
await agent.putRecord({
279
+
repo: agent.accountDid,
280
+
collection: 'com.example.status',
285
+
ctx.logger.warn({ err }, 'failed to write record')
286
+
return res.status(500).json({ error: 'Failed to write record' })
289
+
res.status(200).json({})
294
+
Now in our homepage we can list out the status buttons:
297
+
<!-- src/pages/home.ts -->
298
+
<div class="status-options">
299
+
${['👍', '🦋', '🥳', /*...*/].map(status => html`
300
+
<div class="status-option" data-value="${status}">
307
+
And write some client-side javascript to submit the status on click:
310
+
/* src/pages/public/home.js */
311
+
Array.from(document.querySelectorAll('.status-option'), (el) => {
312
+
el.addEventListener('click', async (ev) => {
313
+
const res = await fetch('/status', {
315
+
headers: { 'content-type': 'application/json' },
316
+
body: JSON.stringify({ status: el.dataset.value }),
318
+
const body = await res.json()
319
+
if (!body?.error) {
326
+
## Step 5. Creating a custom "status" schema
328
+
The collections are typed, meaning that they have a defined schema. The `app.bsky.actor.profile` type definition [can be found here](https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/actor/profile.json).
330
+
Anybody can create a new schema using the [Lexicon](#todo) language, which is very similar to [JSON-Schema](#todo). The schemas use [reverse-DNS IDs](#todo) which indicate ownership, but for this demo app we're going to use `com.example` which is safe for non-production software.
332
+
> ### Why create a schema?
334
+
> Schemas help other applications understand the data your app is creating. By publishing your schemas, you enable compatibility and reduce the chances of bad data affecting your app.
336
+
Let's create our schema in the `/lexicons` folder of our codebase. You can [read more about how to define schemas here](#todo).
339
+
/* lexicons/status.json */
342
+
"id": "com.example.status",
346
+
"key": "literal:self",
349
+
"required": ["status", "updatedAt"],
359
+
"format": "datetime"
368
+
Now let's run some code-generation using our schema:
371
+
./node_modules/.bin/lex gen-server ./src/lexicon ./lexicons/*
374
+
This will produce Typescript interfaces as well as runtime validation functions that we can use in our `POST /status` route:
377
+
/** src/routes.ts **/
378
+
import * as Status from '#/lexicon/types/com/example/status'
380
+
// "Set status" handler
383
+
handler(async (req, res) => {
386
+
// Construct & validate their status record
388
+
$type: 'com.example.status',
389
+
status: req.body?.status,
390
+
updatedAt: new Date().toISOString(),
392
+
if (!Status.validateRecord(record).success) {
393
+
return res.status(400).json({ error: 'Invalid status' })
401
+
## Step 6. Listening to the firehose
405
+
- Logged in via OAuth
406
+
- Created a custom schema
407
+
- Read & written records for the logged in user
409
+
Now we want to fetch the status records from other users.
411
+
Remember how we referred to our app as being like a Google, crawling around the repos to get their records? One advantage we have in the AT Protocol is that each repo publishes an event log of their updates.
415
+
│ REPO │ Event stream
417
+
│ ┌───────────────────────────────────────────┐
418
+
├───┼ 1 PUT /com.example.status/self │
419
+
│ └───────────────────────────────────────────┘
420
+
│ ┌───────────────────────────────────────────┐
421
+
├───┼ 2 DEL /app.bsky.feed.post/3l244rmrxjx2v │
422
+
│ └───────────────────────────────────────────┘
423
+
│ ┌───────────────────────────────────────────┐
424
+
├───┼ 3 PUT /app.bsky.actor/self │
425
+
▼ └───────────────────────────────────────────┘
428
+
Using a [Relay service](#todo) we can listen to an aggregated firehose of these events across all users in the network. In our case what we're looking for are valid `com.example.status` records.
432
+
/** src/firehose.ts **/
433
+
import * as Status from '#/lexicon/types/com/example/status'
435
+
const firehose = new Firehose({})
437
+
for await (const evt of firehose.run()) {
438
+
// Watch for write events
439
+
if (evt.event === 'create' || evt.event === 'update') {
440
+
const record = evt.record
442
+
// If the write is a valid status update
444
+
evt.collection === 'com.example.status' &&
445
+
Status.isRecord(record) &&
446
+
Status.validateRecord(record).success
448
+
// Store the status
455
+
Let's create a SQLite table to store these statuses:
459
+
// Create our statuses table
461
+
.createTable('status')
462
+
.addColumn('authorDid', 'varchar', (col) => col.primaryKey())
463
+
.addColumn('status', 'varchar', (col) => col.notNull())
464
+
.addColumn('updatedAt', 'varchar', (col) => col.notNull())
465
+
.addColumn('indexedAt', 'varchar', (col) => col.notNull())
469
+
Now we can write these statuses into our database as they arrive from the firehose:
472
+
/** src/firehose.ts **/
473
+
// If the write is a valid status update
475
+
evt.collection === 'com.example.status' &&
476
+
Status.isRecord(record) &&
477
+
Status.validateRecord(record).success
479
+
// Store the status in our SQLite
481
+
.insertInto('status')
483
+
authorDid: evt.author,
484
+
status: record.status,
485
+
updatedAt: record.updatedAt,
486
+
indexedAt: new Date().toISOString(),
488
+
.onConflict((oc) =>
489
+
oc.column('authorDid').doUpdateSet({
490
+
status: record.status,
491
+
updatedAt: record.updatedAt,
492
+
indexedAt: new Date().toISOString(),
499
+
You can almost think of information flowing in a loop:
502
+
┌─────Repo put─────┐
504
+
┌──────┴─────┐ ┌───────────┐
505
+
│ App server │ │ User repo │
506
+
└────────────┘ └─────┬─────┘
508
+
└────Event log─────┘
511
+
Why read from the event log? Because there are other apps in the network that will write the records we're interested in. By subscribing to the event log, we ensure that we catch all the data we're interested in -- including data published by other apps.
513
+
## Step 7. Listing the latest statuses
515
+
Now that we have statuses populating our SQLite, we can produce a timeline of status updates by users. We also use a [DID](#todo)-to-handle resolver so we can show a nice username with the statuses:
518
+
/** src/routes.ts **/
522
+
handler(async (req, res) => {
525
+
// Fetch data stored in our SQLite
526
+
const statuses = await db
527
+
.selectFrom('status')
529
+
.orderBy('indexedAt', 'desc')
533
+
// Map user DIDs to their domain-name handles
534
+
const didHandleMap = await resolver.resolveDidsToHandles(
535
+
statuses.map((s) => s.authorDid)
543
+
Our HTML can now list these status records:
546
+
<!-- src/pages/home.ts -->
547
+
${statuses.map((status, i) => {
548
+
const handle = didHandleMap[status.authorDid] || status.authorDid
549
+
const date = ts(status)
551
+
<div class="status-line">
553
+
<div class="status">${status.status}</div>
556
+
<a class="author" href="https://bsky.app/profile/${handle}">@${handle}</a>
557
+
was feeling ${status.status} on ${status.indexedAt}.
564
+
## Step 8. Optimistic updates
566
+
As a final optimization, let's introduce "optimistic updates." Remember the information flow loop with the repo write and the event log? Since we're updating our users' repos locally, we can short-circuit that flow to our own database:
569
+
┌───Repo put──┬──────┐
571
+
┌──────┴─────┐ │ ┌───────────┐
572
+
│ App server │◄──────┘ │ User repo │
573
+
└────────────┘ └───┬───────┘
575
+
└────Event log───────┘
578
+
This is an important optimization to make, because it ensures that the user sees their own changes while using your app. When the event eventually arrives from the firehose, we just discard it since we already have it saved locally.
580
+
To do this, we just update `POST /status` to include an additional write to our SQLite DB:
583
+
/** src/routes.ts **/
584
+
// "Set status" handler
587
+
handler(async (req, res) => {
591
+
// Write the status record to the user's repository
592
+
await agent.putRecord({
593
+
repo: agent.accountDid,
594
+
collection: 'com.example.status',
599
+
ctx.logger.warn({ err }, 'failed to write record')
600
+
return res.status(500).json({ error: 'Failed to write record' })
604
+
// Optimistically update our SQLite <-- HERE!
606
+
.insertInto('status')
608
+
authorDid: agent.accountDid,
609
+
status: record.status,
610
+
updatedAt: record.updatedAt,
611
+
indexedAt: new Date().toISOString(),
613
+
.onConflict((oc) =>
614
+
oc.column('authorDid').doUpdateSet({
615
+
status: record.status,
616
+
updatedAt: record.updatedAt,
617
+
indexedAt: new Date().toISOString(),
624
+
'failed to update computed view; ignoring as it should be caught by the firehose'
628
+
res.status(200).json({})
633
+
You'll notice this code looks almost exactly like what we're doing in `firehose.ts`.
643
+
Let's create the client during the server init:
647
+
import { NodeOAuthClient } from '@atproto/oauth-client-node'
649
+
// static async create() {
652
+
const publicUrl = env.PUBLIC_URL
653
+
const url = publicUrl || `http://127.0.0.1:${env.PORT}`
654
+
const oauthClient = new NodeOAuthClient({
656
+
client_name: 'AT Protocol Express App',
657
+
client_id: publicUrl
658
+
? `${url}/client-metadata.json`
659
+
: `http://localhost?redirect_uri=${encodeURIComponent(`${url}/oauth/callback`)}`,
661
+
redirect_uris: [`${url}/oauth/callback`],
662
+
scope: 'profile offline_access',
663
+
grant_types: ['authorization_code', 'refresh_token'],
664
+
response_types: ['code'],
665
+
application_type: 'web',
666
+
token_endpoint_auth_method: 'none',
667
+
dpop_bound_access_tokens: true,
669
+
stateStore: new StateStore(db),
670
+
sessionStore: new SessionStore(db),
677
+
There's quite a bit of configuration which is [explained in the OAuth guide](#todo). We host that config at `/client-metadata.json` as part of the OAuth flow.
680
+
/** src/routes.ts **/
684
+
'/client-metadata.json',
685
+
handler((_req, res) => {
686
+
return res.json(oauthClient.clientMetadata)
695
+
We're going to need to track two kinds of information:
697
+
- **OAuth State**. This is information about login flows that are in-progress.
698
+
- **OAuth Sessions**. This is the active session data.
700
+
The `oauth-client-node` library handles most of this for us, but we need to create some tables in our SQLite to store it. Let's update `/src/db.ts` for this.
706
+
export type DatabaseSchema = {
707
+
auth_session: AuthSession
708
+
auth_state: AuthState
711
+
export type AuthSession = {
713
+
session: string // JSON
716
+
export type AuthState = {
718
+
state: string // JSON
723
+
migrations['001'] = {
724
+
async up(db: Kysely<unknown>) {
726
+
.createTable('auth_session')
727
+
.addColumn('key', 'varchar', (col) => col.primaryKey())
728
+
.addColumn('session', 'varchar', (col) => col.notNull())
731
+
.createTable('auth_state')
732
+
.addColumn('key', 'varchar', (col) => col.primaryKey())
733
+
.addColumn('state', 'varchar', (col) => col.notNull())
736
+
async down(db: Kysely<unknown>) {
737
+
await db.schema.dropTable('auth_state').execute()
738
+
await db.schema.dropTable('auth_session').execute()
749
+
Data in the Atmosphere is stored on users' personal servers. It's almost like each user has their own website. Our goal is to aggregate data from the users into our SQLite.
751
+
Think of our app like a Google. If Google's job was to say which emoji each website had under `/status.txt`, then it would show something like:
753
+
- `nytimes.com` is feeling 📰 according to `https://nytimes.com/status.txt`
754
+
- `bsky.app` is feeling 🦋 according to `https://bsky.app/status.txt`
755
+
- `reddit.com` is feeling 🤓 according to `https://reddit.com/status.txt`
757
+
The Atmosphere works the same way, except we're going to check `at://nytimes.com/com.example.status/self`. Literally, that's it! Each user has a domain, and each record gets published under an atproto URL.
762
+
at://nytimes.com/com.example.status/self
764
+
The user The data type The record name
767
+
When somebody logs into our app, they'll give read & write access to their personal `at://`. We'll use that to write the `/com.example.status/self` record. Then we'll crawl the Atmosphere for all the other `/com.example.status/self` records, and aggregate them into our SQLite database for fast reads.
769
+
Believe it or not, that's how most apps on the Atmosphere are built, including [Bluesky](#todo).