DEV Community

GraphQL subscriptions with Nest: how to publish across multiple running servers

Georgii Rychko on September 24, 2019

Today we'll learn how to setup GraphQL (GQL) subscriptions using Redis and NestJS. Prerequisites for this article: An experience in GraphQL So...
Collapse
 
masterofdesaster3 profile image
MasterofDesaster3

Hello Georgii Rychko, I followed your tutorial to learn about graphql subscriptions and at the end i had the same code you have here without the part multiple ports. But Graphql allways throws an error when i want to subscribe on port 3000 (my app also runs on 30) with:
{
"error": {
"message": "Cannot read property 'headers' of undefined"
}
}
Do you have an idea why? Thank you. Great Tutorial!

Collapse
 
dcsan profile image
dc

Does postgres backed pub/sub also provide this type of event driven data sync? I thought postgres itself has emitter type functionality so the graphql code layer would then need to use sockets for communication?

Collapse
 
rychkog profile image
Georgii Rychko This Dot

The Pub/Sub that is available in PostgreSQL would also work but you might create an extra load on the db server by making it responsible for doing regular data manipulation stuff + pub/sub.

In general you're right and in order to start using PostgreSQL as a pub/sub engine one need to implement Apollo's PubSubEngine interface. Here is, for instance, Redis subscription implementation: github.com/davidyaha/graphql-redis...

Collapse
 
sanzhardanybayev profile image
Sanzhar Danybayev

Thanks for the detailed explanation. Original docs are not precise & clear enough.

Collapse
 
sk8guerra profile image
Jorge Guerra

Great post, this is exactly what I was looking for.
Do you know how can I listen to these Subscriptions on a Reactjs app?

Collapse
 
draylegend profile image
Vladimir Drayling

I think it could be helpful. Awesome lib: apollographql.com/docs/react

Collapse
 
mikingtheviking profile image
MikingTheViking

Great post! Thank you!

Collapse
 
ben profile image
Ben Halpern

Great post

Collapse
 
rychkog profile image
Georgii Rychko This Dot

Thanks!