markhuot / craftql

A drop-in GraphQL server for Craft CMS
Other
319 stars 53 forks source link

Subscription support #93

Open chrisrowe opened 6 years ago

chrisrowe commented 6 years ago

I've very much been enjoying using CraftQL but wondered if subscriptions and WS support had been considered, is planned, or is outside of the scope of what Craft plugins can support?

markhuot commented 6 years ago

It's possible, but tricky. Are you using subscription support elsewhere? I'm curious how you've interacted with it.

The hardest part would be supporting subscriptions entirely within Craft. E.g., ideally you'd send a subscription request through GraphQL and get back some sort of endpoint that you could poll or make long lived requests to… but that's sort of gross because then PHP would have to support those long lived connections, which (even in 7) it's not great at. So we could involve another technology like Redis or some messaging platform but then we start to get outside what a vanilla Craft plugin can do.

All that said, this is something I'd like to support. If you can walk me through how you'd use it I'm happy to discuss it more.

chrisrowe commented 6 years ago

I've used subscriptions elsewhere and enjoyed the benefits. It's by no means a requirement for my current project, and a refresh button or implement polling on the front end will suffice to bring in new data.

I have a large number of sales agents entering data into a SPA which is essentially a CRM, they will be running this in the browser all day and so pushing new data to them would be ideal.

markhuot commented 6 years ago

Thanks Chris. In your implementation do you have access to any sort of streaming service? E.g.,

chrisrowe commented 6 years ago

I have no message queue implemented currently and had anticipated using node. FB's approach seems ideal since it's additive

khalwat commented 6 years ago

I'd love to see subscriptions as well; currently I'm polling, and it works okay... but the app is a perfect fit for GraphQL subscriptions https://www.howtographql.com/graphql-js/7-subscriptions/

It's unfortunate that PHP might be a weak link here.

markhuot commented 6 years ago

So I've been thinking on this implementation and I'm curious what you all are looking to "subscribe" to. One of my favorite ways to implement this would be to build off of the built in Craft events and offer a new top level field, like this:

{
  subscribe {
    beforeSaveElement {
      # Returns an `ElementInterface`. This would fire when ever an element is
      # saved and you could pull out whatever fields/data you want. E.g.,
      ...on ElementType {
        someFieldOnYourElement
      }
    }
}

That could, theoretically work for any event,

{
  subscribe {
    beforeSaveElement { ... }
    afterSaveElement { ... }
    beforeUpdateSlugAndUri { ... }
    beforeDeleteElement { ... }
}

This means you'd only be able to subscribe to the things that Craft currently exposes as events, but that seems like the most performant way to approach this.


@khalwat, makes a good point that PHP probably isn't the best language to open long lived HTTP connections… but ¯\(ツ)/¯ I think I'll start here and see where it takes me.

jasonmccallister commented 5 years ago

So this is Laravel specific, but uses Ratchet underneath... https://github.com/beyondcode/laravel-websockets

michieldewilde commented 5 years ago

What is the status on this? And what are the pain points atm? ping @echelpoel

jasonmccallister commented 5 years ago

@markhuot like @khalwat states, PHP is not the best approach for these connections, is it an absolute crazy idea to recommend a web socket middleware that does the work and connects to the PHP connection? Maybe even outside of this plugin?