Skip to content

Commit

Permalink
Merge pull request #50 from cloudflare/use-rpc-do
Browse files Browse the repository at this point in the history
use DurableObject from cloudflare:workers
  • Loading branch information
threepointone authored Jun 14, 2024
2 parents 42bf279 + e39924e commit facb633
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
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

0 comments on commit facb633

Please sign in to comment.