nuxt-hub / core

Build full-stack applications with Nuxt on CloudFlare, with zero configuration.
https://hub.nuxt.com
Apache License 2.0
812 stars 36 forks source link

Queues support #135

Open Atinux opened 1 month ago

Atinux commented 1 month ago

See https://developers.cloudflare.com/queues/

How would you see the usage within NuxtHub? Feedback welcome ❤️

RihanArfan commented 2 weeks ago

Consumers could be defined similar to other Nitro events in server/queues/consumer-example.ts. Would probs be like Nitro's event object syntax so that details to configure it can be done alongside the code.

For producers, a composable like hubQueues().send() would suffice. Could have some fancy DX around autocompletion for queue bindings both in consumer objects and producer function.

It's worth noting that Cloudflare are introducing a thing for workflows which I think should be considered alongside implementing queues as it may be better long term for handling complex applications better? See this Tweet and this blog post.

Atinux commented 2 weeks ago

Consumers could be defined similar to other Nitro events in server/queues/consumer-example.ts. Would probs be like Nitro's event object syntax so that details to configure it can be done alongside the code.

I really like this idea!

Regarding the workflow, would you mind creating a new issue for it and linking this one too?

Gerbuuun commented 2 weeks ago

I'm not sure how this works with pages? Currently I have a separate worker project which is the queue consumer. Does pages support this or is it only pull based (http)?

It would be nice to be able to merge this consumer code back into my main Nuxt project!

Atinux commented 1 week ago

I am not sure that Pages support queues but I know it's planned.

I try to avoid having to create two outputs and 2 deployments for one project.

RihanArfan commented 1 week ago

I'm not sure how this works with pages? Currently I have a separate worker project which is the queue consumer. Does pages support this or is it only pull based (http)?

It would be nice to be able to merge this consumer code back into my main Nuxt project!

I believe you're right where a consumer could only be a separate Worker, or be HTTP pull based.

With Pages, currently you can only add a producer binding. image

valtlfelipe commented 1 week ago

I'm sorry if it's not the right place to ask, but I would like to publish a message on a CF queue from a hub project. But how would that be possible if we haven't the binding in hub yet, and I don't have access to the wrangler file to create de binding manually? Anyone had to do this already?

Also, if I may contribute with my feedback, Pages currently supports producers, and there is an HTTP API for pulling and ack messages. Simple composables, similar to the KV, would be really nice and simple to use.

Gerbuuun commented 1 week ago

@valtlfelipe I use this util to publish to a Queue (based on other nuxthub utils)

import type { Queue } from '@cloudflare/workers-types/experimental';

interface Message {
  orderId: string;
  lang: string;
}
type OrderQueue = Queue<Message>;

let queue: OrderQueue;

export function useQueue() {
  if (queue)
    return queue;

  // @ts-expect-error globalThis.__env__ is not defined
  // eslint-disable-next-line node/prefer-global/process
  const binding = process.env.QUEUE || globalThis.__env__?.QUEUE || globalThis.QUEUE;

  if (binding) {
    queue = binding as OrderQueue;
    return queue;
  }

  throw createError('Cloudflare Queue binding not found');
}

You should add the Queue binding to the worker in the cloudflare dashboard manually.

valtlfelipe commented 1 week ago

@valtlfelipe I use this util to publish to a Queue (based on other nuxthub utils) [...] You should add the Queue binding to the worker in the cloudflare dashboard manually.

Nice workaround. Thanks!