Can anyone help me with a working example of graphql subscription with php5 and AWS Appsync or php5 WebSocket client which works with Appsync? I am trying textalk but haven't got any success.
<?php
require(dirname(dirname(__FILE__)) . '/graphql/vendor/autoload.php');
use WebSocket\Client;
$query = <<<'GRAPHQL'
subscription onCreateProfile{
onCreateBatch {
hotelId
batchId
profiles {
id
}
}
}
GRAPHQL;
$appSyncURL = 'wss://pbnblnr7xxxxxxxxxxxx.appsync-realtime-api.ap-south-1.amazonaws.com/graphql';
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'verify_peer', false);
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
$client = new WebSocket\Client($appSyncURL, [
'timeout' => 60, // 1 minute time out
'context' => $context,
'headers' => [
'x-api-key' => 'APIKEY',
],
]);
$client->send($query);
echo $client->receive();
$client->close();
Running above snippet gives below error:
$ php subscription.php
{"payload":{"errors":[{"message":"NoProtocolError","errorCode":400}]},"type":"connection_error"}PHP Fatal error: Uncaught WebSocket\ConnectionException: Empty read; connection dead? Stream state: {"crypto":{"protocol":"UNKNOWN","cipher_name":"TLS_AES_256_GCM_SHA384","cipher_bits":256,"cipher_version":"TLSv1.3"},"timed_out":false,"blocked":true,"eof":true,"stream_type":"tcp_socket\/ssl","mode":"r+","unread_bytes":0,"seekable":false} in /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php:316
Stack trace:
#0 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(299): WebSocket\Base->throwException('Empty read; con...')
#1 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(159): WebSocket\Base->read(2)
#2 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(149): WebSocket\Base->receiveFragment()
#3 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(272): WebSocket\Base->receive()
#4 /var/www/html/demo/graphql/subscription.php(31): WebSocket\Base->close()
#5 {main}
thrown in /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php on line 316
So has anyone tried graphQL subscriptions with php5? My requirement is to consume Graphql Subscription in PHP application (in backend). I know graphQL subscription would work fine in frontend through javascript WebSocket client. But I've to use graphql subscriptions in PHP5 application.
Any help will be appreciated!
I've figured it out and now it is working fine with >= PHP 5.6.
Here is the working example of the same, in case anyone needs it.
https://github.com/roshanlabh/php-appsync-consumer-example/blob/master/appsync-subscription.php