Websocket Server with Durable Object Client Connection

Hi, I try build a websocket server with durable object hibernation api, on the server side I think its considered done, but on the client side, how we connect to the durable object using service binding ? I'm using cloudflare pages and want to connect to the websocket server using service binding anyone have code snippet to get address already connected on the wrangler side, but I'm strugle to get the websocket service endpoint dynamically from service binding any advise ?
6 Replies
Kholis
KholisOP3w ago
have handler to return
case '/init': {
return new Response(
JSON.stringify({
hostname: url.hostname,
port: url.port || (url.protocol === 'https:' ? '443' : '80'),
secure: url.protocol === 'https:'
}),
{
headers: {
'Content-Type': 'application/json'
}
}
);
}
case '/init': {
return new Response(
JSON.stringify({
hostname: url.hostname,
port: url.port || (url.protocol === 'https:' ? '443' : '80'),
secure: url.protocol === 'https:'
}),
{
headers: {
'Content-Type': 'application/json'
}
}
);
}
can be curl-ed
❯ curl http://labs-dev.kholisrag.com:8000/init
{"hostname":"labs-dev.kholisrag.com","port":"80","secure":false}%
❯ curl http://labs-dev.kholisrag.com:8000/init
{"hostname":"labs-dev.kholisrag.com","port":"80","secure":false}%
but on pages side
const doNamespace = platform.env.DOWebsocket;
const id = doNamespace.idFromName('crossws');
const websocketDO = doNamespace.get(id);

// Fetch configuration from Durable Object
const response = await websocketDO.fetch('http://dummy-host/init');

if (!response.ok) {
console.warn('Failed to fetch WebSocket config, using development config');
throw new Error('Failed to fetch WebSocket config');
}

const config = await response.json<{
hostname: string;
port: string;
secure: boolean;
}>();
const doNamespace = platform.env.DOWebsocket;
const id = doNamespace.idFromName('crossws');
const websocketDO = doNamespace.get(id);

// Fetch configuration from Durable Object
const response = await websocketDO.fetch('http://dummy-host/init');

if (!response.ok) {
console.warn('Failed to fetch WebSocket config, using development config');
throw new Error('Failed to fetch WebSocket config');
}

const config = await response.json<{
hostname: string;
port: string;
secure: boolean;
}>();
seem not working.
No description
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Kholis
KholisOP3w ago
Worker handling the request (Edit: this will be your pages function):
actually I need it on the pages itself / client side. not the page function
Websocket DO:
yeah already do like this. The things is we have a CF Worker - Websocket DO I need my CF Pages / any other client / agent there to connect to websocket server. on the Pages side, since there is service binding, we don't need to connect outside of cloudflare network. so when end user open my pages, they can do websocket connection to the web server and reach / communicate with others.
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Kholis
KholisOP3w ago
basically our CF Pages will act as a Proxy to our DOWebsocket ya? got this error before
Error fetching WebSocket config: Error: Cannot access `DOWebsocket#webSocketAddress` as Durable Object RPC is not yet supported between multiple `wrangler dev` sessions.
Error fetching WebSocket config: Error: Cannot access `DOWebsocket#webSocketAddress` as Durable Object RPC is not yet supported between multiple `wrangler dev` sessions.
does it guaranted it will work on the prod itself?
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?