Open chrisrowe opened 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.
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.
Thanks Chris. In your implementation do you have access to any sort of streaming service? E.g.,
I have no message queue implemented currently and had anticipated using node. FB's approach seems ideal since it's additive
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.
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.
So this is Laravel specific, but uses Ratchet underneath... https://github.com/beyondcode/laravel-websockets
What is the status on this? And what are the pain points atm? ping @echelpoel
@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?
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?