Skip to content

Cloudflare Websocket with Durable Objects not working #15975

@LawrenceGB

Description

@LawrenceGB

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/cloudflare

SDK Version

9.11.0

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

import { DurableObject } from "cloudflare:workers";
import * as Sentry from '@sentry/cloudflare';

// Worker with Sentry wrapper
export default Sentry.withSentry(
	env => ({
		dsn: env.SENTRY_DSN,

	}),
	{
		async fetch(request, env, ctx) {
			// Handle WebSocket requests
			if (request.url.includes("/websocket")) {
				// Get DO stub
				const id = env.WEBSOCKET_SERVER.idFromName("user-id");
				const stub = env.WEBSOCKET_SERVER.get(id);

				// Forward to DO
				return await stub.fetch(request);
			}

			return new Response("Not a WebSocket request");
		},
	}
);

// Durable Object for WebSocket
export class WebSocketServer extends DurableObject {
	constructor(ctx, env) {
		super(ctx, env);
	}

	async fetch(request) {
		// Verify WebSocket upgrade
		const upgradeHeader = request.headers.get("Upgrade");
		if (!upgradeHeader || upgradeHeader.toLowerCase() !== "websocket") {
			return new Response("Expected WebSocket", { status: 426 });
		}

		// Create WebSocket pair
		const webSocketPair = new WebSocketPair();
		const [client, server] = Object.values(webSocketPair);

		// Accept the server side
		server.accept();

		// Basic message handler
		server.addEventListener("message", (event) => {
			server.send("Echo: " + event.data);
		});

		Sentry.captureException(new Error('Testing sentry'));


		// Return client WebSocket
		return new Response(null, {
			status: 101,
			webSocket: client
		});
	}
}

Steps to Reproduce

Just added a test exception, did not show up.

Expected Result

Should have received the error in dashboard.

Actual Result

Did not show.
However, it does work when running the worker locally.

Activity

added theissue type on Apr 3, 2025
moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Apr 3, 2025
Lms24

Lms24 commented on Apr 4, 2025

@Lms24
Member

Hey @LawrenceGB I believe your issue is a duplicate to #15342. I'm not fully involved in our CloudFlare SDK (therefore tagging @AbhiPrasad) but as far as I can tell, the SDK is not initialized in your WebSocketServer instance.
In the issue, someone posted a workaround for durable objects. Would you mind giving this a try?

moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Apr 4, 2025
changed the issue type fromtoon Apr 4, 2025
moved this to Waiting for: Community in GitHub Issues with 👀 3on Apr 4, 2025
AbhiPrasad

AbhiPrasad commented on Apr 4, 2025

@AbhiPrasad
Member

Yup, we don't wrap DurableObjects yet, we can backlog this as a feature, but the workaround should work as expected.

18 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Status

Waiting for: Community

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Cloudflare Websocket with Durable Objects not working · Issue #15975 · getsentry/sentry-javascript