Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use DurableObject from cloudflare:workers #50

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions app/durableObjects/ChatRoom.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { assertNonNullable } from '~/utils/assertNonNullable'
import getUsername from '~/utils/getUsername.server'
import { handleErrors } from '~/utils/handleErrors.server'

import { DurableObject } from 'cloudflare:workers'

type Session = {
heartbeatTimeout: ReturnType<typeof setTimeout> | null
webSocket?: WebSocket
Expand All @@ -28,7 +30,7 @@ type Session = {
// ChatRoom implements a Durable Object that coordinates an individual chat room. Participants
// connect to the room using WebSockets, and the room broadcasts messages from each participant
// to all others.
export class ChatRoom implements DurableObject {
export class ChatRoom extends DurableObject {
/**
* WebSocket objects for each client, along with some metadata
*/
Expand All @@ -42,15 +44,8 @@ export class ChatRoom implements DurableObject {
lastTimestamp: number = 0
stateSyncInterval: ReturnType<typeof setInterval> | null = null

// We'd like to use Cloudflare's fancy RPC Durable Objects
// but currently we can't figure out a way to mark cloudflare:workers
// as an external when remix compiles it. Until then, let's match the
// newer style member names .ctx and .env
ctx: DurableObjectState
env: Env
constructor(ctx: DurableObjectState, env: Env) {
this.ctx = ctx
this.env = env
super(ctx, env)
// check for previous sessions.
this.ctx.blockConcurrencyWhile(async () => {
this.sessions = (await this.ctx.storage.get<Session[]>('sessions')) ?? []
Expand Down
4 changes: 3 additions & 1 deletion remix.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module.exports = {
ignoredRouteFiles: ['**/.*'],
server: './server.ts',
serverConditions: ['worker'],
serverDependenciesToBundle: [/^(?!__STATIC_CONTENT_MANIFEST).*$/],
serverDependenciesToBundle: [
/^(?!__STATIC_CONTENT_MANIFEST|cloudflare:workers).*$/,
],
serverMainFields: ['browser', 'module', 'main'],
serverMinify: true,
serverModuleFormat: 'esm',
Expand Down