-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriptions.js
54 lines (40 loc) · 1.15 KB
/
subscriptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//INCLUDE THIS IN HTML
// <script
// src="https://unpkg.com/graphql-ws/umd/graphql-ws.min.js"
// type="application/javascript"
// ></script>
let wsClient = null;
const disconnectSubscription=()=>{
wsClient?.dispose();
return true;
}
const createSubscription=async (query,auth_token)=>{
wsClient = graphqlWs.createClient({
url: "ws://localhost:3002/graphql",
connectionParams:{
auth_token
},
lazy:false
});
wsClient.on('connecting',()=>{
console.log('connecting subscription')
})
wsClient.on('connected',async ()=>{
console.log('subscription connected');
//Callback to godot to inform about socket connected;
const subscription = wsClient.iterate({
query,
});
for await (const event of subscription) {
//Callback to godot for sending data
console.log(event);
}
})
wsClient.on('error',(err)=>{
console.log('error in subscription',err);
})
wsClient.on('closed',()=>{
//callback to godot to inform disconnection if needed
disconnectSubscription()
})
}