@@ -21,7 +21,7 @@ A PHP client for the [NATS messaging system](https://nats.io).
2121Requirements
2222------------
2323
24- * php 5.5 +
24+ * php 5.6 +
2525* [ nats] ( https://github.com/derekcollison/nats ) or [ gnatsd] ( https://github.com/apcera/gnatsd )
2626
2727
@@ -58,30 +58,93 @@ Composer will download all the dependencies defined in composer.json, and prepar
5858$client = new \Nats\Connection();
5959$client->connect();
6060
61- # Simple Publisher
62- $client->publish("foo", "foo bar");
61+ // Publish Subscribe
6362
64- # Simple Subscriber
65- $callback = function($payload)
66- {
67- printf("Data: %s\r\n", $payload);
68- };
69- $client->subscribe("foo", $callback);
63+ // Simple Subscriber.
64+ $client->subscribe(
65+ 'foo',
66+ function ($message) {
67+ printf("Data: %s\r\n", $message->getBody());
68+ }
69+ );
7070
71- # Request
72- $client->request('sayhello', 'Marty McFly', function ($response) {
73- echo $response->getBody();
74- });
71+ // Simple Publisher.
72+ $client->publish('foo', 'Marty McFly');
7573
76- # Responding to requests
77- $sid = $client->subscribe("sayhello", function ($res) {
78- $res->reply("Hello, " . $res->getBody() . " !!!");
79- });
74+ // Wait for 1 message.
75+ $client->wait(1);
8076
77+ // Request Response
78+
79+ // Responding to requests.
80+ $sid = $client->subscribe(
81+ 'sayhello',
82+ function ($message) {
83+ $message->reply('Reply: Hello, '.$message->getBody().' !!!');
84+ }
85+ );
86+
87+ // Request.
88+ $client->request(
89+ 'sayhello',
90+ 'Marty McFly',
91+ function ($message) {
92+ echo $message->getBody();
93+ }
94+ );
95+ ```
8196
97+ ### Encoded Connections
8298
83- # Wait for 1 message
99+ ``` php
100+ $encoder = new \Nats\Encoders\JSONEncoder();
101+ $options = new \Nats\ConnectionOptions();
102+ $client = new \Nats\EncodedConnection($options, $encoder);
103+ $client->connect();
104+
105+ // Publish Subscribe
106+
107+ // Simple Subscriber.
108+ $client->subscribe(
109+ 'foo',
110+ function ($payload) {
111+ printf("Data: %s\r\n", $payload->getBody()[1]);
112+ }
113+ );
114+
115+ // Simple Publisher.
116+ $client->publish(
117+ 'foo',
118+ [
119+ 'Marty',
120+ 'McFly',
121+ ]
122+ );
123+
124+ // Wait for 1 message.
84125$client->wait(1);
126+
127+ // Request Response
128+
129+ // Responding to requests.
130+ $sid = $client->subscribe(
131+ 'sayhello',
132+ function ($message) {
133+ $message->reply('Reply: Hello, '.$message->getBody()[1].' !!!');
134+ }
135+ );
136+
137+ // Request.
138+ $client->request(
139+ 'sayhello',
140+ [
141+ 'Marty',
142+ 'McFly',
143+ ],
144+ function ($message) {
145+ echo $message->getBody();
146+ }
147+ );
85148```
86149
87150
0 commit comments