Skip to content

Commit

Permalink
server api: add Server satisfies Party.Worker; to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
threepointone committed Jan 3, 2024
1 parent 964e129 commit 5ba5c33
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions apps/docs/src/content/docs/reference/partyserver-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ Because the static `onBeforeConnect` method runs in an edge worker near the user

Related reading: [How PartyKit works](/how-partykit-works)

## Party.Worker

The `Party.Worker` interface describes the `static` methods available on the `Party.Server` class. You can use it to add additional type safety to your TypeScript code:

```ts
import type * as Party from "partykit/server";
export default class Server implements Party.Server {
static async onFetch(req: Party.Request) {
return new Response(req.url);
}
}

Server satisfies Party.Worker;
```

### `static` onFetch

Runs on any HTTP request that does not match a Party URL or a static asset. Useful for running lightweight HTTP endpoints that don't need access to the `Party` state.
Expand All @@ -237,6 +252,8 @@ export default class Server implements Party.Server {
return new Response(req.url, { status: 403 });
}
}

Server satisfies Party.Worker;
```

Because the static `onFetch` method runs in an edge worker near the user instead of in the room, it doesn't have access to `Party` room resources such as storage. Instead, you can access a subset of its properties via a [`Party.FetchLobby`](#partyfetchlobby).
Expand Down Expand Up @@ -264,6 +281,8 @@ export default class Server implements Party.Server {
socket.send("Hello!");
}
}

Server satisfies Party.Worker;
```

:::danger[Multiple parties and onSocket]
Expand Down Expand Up @@ -298,27 +317,14 @@ export default class Server implements Party.Server {
console.log(`Running cron ${cron.name} at ${cron.scheduledTime}`);
}
}

Server satisfies Party.Worker;
```

:::tip[Local development]
When developing locally, you can test your cron jobs by visiting `http://localhost:1999/__scheduled__?cron=cron-name` in your browser.
:::

## Party.Worker

The `Party.Worker` interface describes the `static` methods available on the `Party.Server` class. You can use it to add additional type safety to your TypeScript code:

```ts
import type * as Party from "partykit/server";
export default class Server implements Party.Server {
static async onFetch(req: Party.Request) {
return new Response(req.url);
}
}

Server satisfies Party.Worker;
```

## Party

Each `Party.Server` instance receives an instance of `Party.Party` as a constructor parameter, and can use it to access the room state and resources such as storage, connections, id, and more.
Expand Down

2 comments on commit 5ba5c33

@vercel
Copy link

@vercel vercel bot commented on 5ba5c33 Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

partykit-docs – ./apps/docs

partykit-docs-partykit.vercel.app
partykit-docs-git-main-partykit.vercel.app
partykit-docs.vercel.app
docs.partykit.io

@vercel
Copy link

@vercel vercel bot commented on 5ba5c33 Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

partykit-blog – ./apps/blog

partykit-blog-git-main-partykit.vercel.app
partykit-blog-partykit.vercel.app
partykit-blog.vercel.app
blog.partykit.io

Please sign in to comment.