How to abort realtime connection when request ends?

I'm building a realtime chat app, and evaluating Ably to use as a Pub/Sub solution in my worker (I have a GraphQL subscription that subscribes to incoming pubsub messages from Ably, does some processing to the payload, then passes it to my React frontend). I noticed that (at least when testing locally) every request would start a new Ably connection, and it wouldn't disconnect. Hence I hit my peak Ably connection limit whilst I was developing. Can you help me resolve this please? I'm using GraphQL Yoga server, but here is a simpler example to reproduce the issue. Note I attempted to add code to detect the "abort" signal, but it's never getting triggered.
import * as Ably from "ably"

const worker: ExportedHandler<Record<string, unknown>> = {
fetch: async (request, env) => {
const signal = request.signal as AbortSignal
signal.addEventListener("abort", () => {
console.log("Request aborted via event listener")
})
signal.onabort = () => {
console.log("Request aborted")
}

const ably = new Ably.Realtime({
key: env.ABLY_API_KEY as string,
})

await ably.connection.once("connected")

return Response.json({ message: "Hello, world!" })
},
}

export default worker
import * as Ably from "ably"

const worker: ExportedHandler<Record<string, unknown>> = {
fetch: async (request, env) => {
const signal = request.signal as AbortSignal
signal.addEventListener("abort", () => {
console.log("Request aborted via event listener")
})
signal.onabort = () => {
console.log("Request aborted")
}

const ably = new Ably.Realtime({
key: env.ABLY_API_KEY as string,
})

await ably.connection.once("connected")

return Response.json({ message: "Hello, world!" })
},
}

export default worker
Thanks! 🙂
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server