Magnaboy
Magnaboy
Explore posts from servers
CDCloudflare Developers
Created by Magnaboy on 12/18/2024 in #workers-help
Can I do what I want with websockets in a worker?
I havent used websockets in workers before and have some trouble understanding how it works. I just want to confirm if what I want to do is possible or not: a long-running websocket server that sends messages based on receiving other external events. My actual end goal is something like this: a client (a browser) connects to my worker acting as a websocket server, and they are fed messages over that WS periodically. They are triggered by certain external events, which is the part I'm having trouble thinking about. For example, say I have a discord bot (running somewhere else) that wants to trigger an event on this worker websocket, how would I do that? Sending a simple webhook to my worker with the event data sure, but how do I feed that through to that websocket connection? Is that the part durable objects helps with? Thank you
2 replies
CDCloudflare Developers
Created by Magnaboy on 11/21/2024 in #workers-help
Slow worker response
My worked, running in a hotpath which simply returns a response from the cache, is taking 150ms and seems very slow. But after calling it a couple times, it becomes fast (30ms). But then after some very short amount of time, e.g. 30 seconds, it goes back to taking to 150ms. Is this just a cold startup time, or is it a bug/issue I can fix? Code:
export default {
async fetch(request, env, ctx): Promise<Response> {
if (request.method === "OPTIONS") {
return handleOptions(env, request);
}
const url = new URL(request.url);
const path = url.pathname.substring(1);

if (path.startsWith("api/")) {
// Irrelevant
}

if (path && !path.startsWith("edit")) {
const startx = performance.now();
const response = await cache.match(getRequestForBio(path));
console.log(`Cache lookup took ${performance.now() - startx}ms`);
// Real logs from worker:
//
// "Cache lookup took 9ms"
// "Cache lookup took 10ms"
// "Cache lookup took 8ms" (rest are the same)
if (response) {
console.log(`Cache lookup for ${path} HIT. Returning cached response.`);
return response;
}
export default {
async fetch(request, env, ctx): Promise<Response> {
if (request.method === "OPTIONS") {
return handleOptions(env, request);
}
const url = new URL(request.url);
const path = url.pathname.substring(1);

if (path.startsWith("api/")) {
// Irrelevant
}

if (path && !path.startsWith("edit")) {
const startx = performance.now();
const response = await cache.match(getRequestForBio(path));
console.log(`Cache lookup took ${performance.now() - startx}ms`);
// Real logs from worker:
//
// "Cache lookup took 9ms"
// "Cache lookup took 10ms"
// "Cache lookup took 8ms" (rest are the same)
if (response) {
console.log(`Cache lookup for ${path} HIT. Returning cached response.`);
return response;
}
Other information: - Worker URL: https://nicebioworker.cf-edge.workers.dev/magnaboy . The response is a small 2kb html page. - Timing/latency as reported by chrome devtools: Request sent: 0.23ms | Waiting for server response: 142.95ms Content download: 0.34ms - Response headers that may be helpful : cf-cache-status:HIT server-timing:=cfL4;desc="proto=TCP&rtt=18223&sent=19&recv=21&lost=0&retrans=0&sent_bytes=4324&recv_bytes=3077&delivery_rate=430668&cwnd=235&unsent_bytes=0&cid=95e6d5e7810fc682&ts=322625&x=0" - Although I also use KV/D1, they are not being called in anyway in this code path. - I am using workers paid. - I don't think I have any cloudflare settings enabled whatsoever that would
1 replies
CDCloudflare Developers
Created by Magnaboy on 9/3/2024 in #workers-help
Question regarding worker caching
No description
2 replies
CDCloudflare Developers
Created by Magnaboy on 10/10/2023 in #pages-help
APNG's are not working
No description
1 replies