You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/ROOT/pages/subscriptions/engines.adoc
+35-23Lines changed: 35 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,14 @@
1
1
[[subscription-engines]]
2
-
= Engines
3
-
4
-
There are different ways to how a GraphQL subscription may be set along with `@neo4j/graphql` server.
2
+
:description: This page describes how a GraphQL subscription may be set along with a @neo4j/graphql server.
3
+
= Subscription engines
5
4
5
+
This page describes different ways to set up a GraphQL subscription along with a `@neo4j/graphql` server.
6
6
7
7
== Default
8
+
8
9
The default behavior is automatically set if the `subscriptions` feature is set to `true`, as described in xref::subscriptions/getting-started.adoc[Getting Started]:
9
10
10
-
[source, javascript]
11
+
[source, javascript, indent=0]
11
12
----
12
13
new Neo4jGraphQL({
13
14
typeDefs,
@@ -18,11 +19,13 @@ new Neo4jGraphQL({
18
19
});
19
20
----
20
21
21
-
This behavior enables a simple subscription system that will work on a single instance, which is ideal for development, testing, and servers that do not require horizontal scaling.
22
+
This behavior enables a simple subscription system that works on a single instance.
23
+
It is ideal for development, testing, and servers that do not require horizontal scaling.
22
24
23
25
[[amqp]]
24
26
== AMQP
25
-
Using subscriptions on a server with multiple instances can be tricky, as described in xref::subscriptions/scaling.adoc[Horizontal Scaling].
27
+
28
+
Using subscriptions on a server with multiple instances can be complex, as described in xref::subscriptions/scaling.adoc[Horizontal scaling].
26
29
Therefore, the recommended approach is to use a PubSub system, which can be achieved with an AMQP broker such as link:https://www.rabbitmq.com/[RabbitMQ].
27
30
This is supported by the link:https://www.npmjs.com/package/@neo4j/graphql-amqp-subscriptions-engine[@neo4j/graphql-amqp-subscriptions-engine] package.
28
31
@@ -36,19 +39,22 @@ Some brokers supporting this protocol are:
@@ -72,33 +78,37 @@ const neoSchema = new Neo4jGraphQL({
72
78
=== API
73
79
The following options can be passed to the constructor:
74
80
75
-
* **connection**: An AMQP uri as a string or a configuration object.
76
-
** **hostname**: Hostname to be used, defaults to `localhost`.
81
+
* **connection**: an AMQP uri as a string or a configuration object.
82
+
** **hostname**: hostname to be used.
83
+
Defaults to `localhost`.
77
84
** **username**: defaults to `guest`.
78
85
** **password**: defaults to `guest`.
79
-
** **port**: Port of the AMQP broker, defaults to `5672`.
80
-
* **exchange**: The exchange to be used in the broker. Defaults to `neo4j.graphql.subscriptions.fx`.
81
-
* **version**: The AMQP version to be used. Currently only `0-9-1` is supported.
86
+
** **port**: port of the AMQP broker.
87
+
Defaults to `5672`.
88
+
* **exchange**: the exchange to be used in the broker.
89
+
Defaults to `neo4j.graphql.subscriptions.fx`.
90
+
* **version**: the AMQP version to be used.
91
+
Currently only `0-9-1` is supported.
82
92
83
93
Additionally, any option supported by link:https://www.npmjs.com/package/amqplib[amqplib] can be passed to `connection`.
84
-
85
-
==== Methods
94
+
To set these configurations up, use the following method:
86
95
87
96
* **close(): Promise<void>**: Closes the connection and channel created, and unbinds the event emitter.
88
97
89
98
[[custom-subscription]]
90
-
== Custom Subscription Engine
99
+
== Custom subscription engine
100
+
91
101
If none of the existing engines is valid for your use case, you can create a new engine to connect to any broker you may need.
92
102
For that, you need to create a new class defining your messaging behavior and it must contain:
93
103
94
104
* An `EventEmitter` property called `events` that should emit an event every time the broker sends a message.
95
-
* A `publish` method that will publish a new event to the broker.
96
-
* Optionally, an `init` method returning a promise that will be called on `getSchema`.
105
+
* A `publish` method that should publish a new event to the broker.
106
+
* Optionally, an `init` method returning a promise that should be called on `getSchema`.
97
107
This is useful for setting up the connection to a broker.
98
108
99
109
In case you want to handle subscriptions using link:https://redis.io/[redis]:
100
110
101
-
[source, javascript]
111
+
[souce, javascript, indent=0]
102
112
----
103
113
// Note: This is an example of a custom subscription behavior and not a production ready redis implementation.
104
114
class CustomRedisSubscriptionEngine {
@@ -139,9 +149,10 @@ const neoSchema = new Neo4jGraphQL({
139
149
----
140
150
141
151
Note that extra properties and methods are often needed to handle the connection to the broker.
142
-
As long as the messages are sent to the broker in the `publish` method and that these messages are received and then emitted through the `events` property, the subscriptions will be properly handled.
152
+
However, as long as the messages are sent to the broker in the `publish` method and that these messages are received and then emitted through the `events` property, the subscriptions are properly handled.
143
153
144
154
=== Using Typescript
155
+
145
156
If using Typescript, you may import the interface `Neo4jGraphQLSubscriptionsEngine` to implement your own class.
146
157
Ensure the API is correctly defined:
147
158
@@ -152,6 +163,7 @@ class CustomRedisEngine implements Neo4jGraphQLSubscriptionsEngine {}
152
163
153
164
[NOTE]
154
165
====
155
-
Events are sent to the class in order, however, order is not guaranteed once these events have been broadcasted through a broker.
166
+
Events are sent in order to the class.
167
+
However, order is not guaranteed once these events have been broadcasted through a broker.
156
168
For cases when ordering is important, you must set up the field `timestamp` in the subscriptions payload.
0 commit comments