-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathappsync-subscription.php
More file actions
70 lines (60 loc) · 2.24 KB
/
appsync-subscription.php
File metadata and controls
70 lines (60 loc) · 2.24 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
// An example to check whether graphql subscription is working or not
require dirname(__FILE__) . '/vendor/autoload.php';
$api_url = 'https://pbnblnr7xxxxxxxxxxxxxxxxx.appsync-api.ap-south-1.amazonaws.com/graphql'; // Appsync-API-URL
$api_key = '<APPSYNC-API-KEY>';
$wss_url = str_replace('https', 'wss', str_replace('appsync-api', 'appsync-realtime-api', $api_url));
$host = str_replace('https://', '', str_replace('/graphql', '', $api_url));
$authorization = ['host' => $host, 'x-api-key' => $api_key];
$api_header = json_encode($authorization);
$base64_api_header = base64_encode($api_header);
$appsync_url = "$wss_url?header=$base64_api_header&payload=e30=";
function uuid4()
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
$client = new WebSocket\Client($appsync_url, [
'timeout' => 60, // 1 minute time out
'headers' => ['sec-websocket-protocol' => 'graphql-ws'],
]);
// connection_init
$client->send(json_encode(["type" => "connection_init"]));
$connectionInitResponse = $client->receive();
$connectionInitArr = json_decode($connectionInitResponse, true);
print_r($connectionInitArr);
if ($connectionInitArr['type'] === 'connection_ack') {
$query = json_encode([
'query' => 'subscription onCreateTodo { onCreateTodo { __typename title description } }',
// 'variables' => '{}',
]);
$startReqArr = [
'id' => uuid4(),
'payload' => [
'data' => $query,
'extensions' => [
'authorization' => $authorization,
],
],
'type' => 'start',
];
// echo json_encode($startReqArr);
$client->send(json_encode($startReqArr));
while (true) {
try {
$startResponse = $client->receive();
$startResArr = json_decode($startResponse, true);
print_r($startResArr);
// Act on received message
// Break while loop to stop listening
} catch (Exception $e) {
// Possibly log errors
echo ($e->getMessage());
}
}
}