Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 199c9cf

Browse files
committed
refactored examples and better README
1 parent 4115fdd commit 199c9cf

7 files changed

Lines changed: 67 additions & 85 deletions

File tree

README.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,32 @@ 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

6463
# Simple Subscriber
65-
$callback = function($payload)
64+
$callback = function($message)
6665
{
67-
printf("Data: %s\r\n", $payload);
66+
printf("Data: %s\r\n", $message->getBody());
6867
};
6968
$client->subscribe("foo", $callback);
7069

71-
# Request
72-
$client->request('sayhello', 'Marty McFly', function ($response) {
73-
echo $response->getBody();
74-
});
70+
# Simple Publisher
71+
$client->publish("foo", "foo bar");
72+
73+
# Wait for 1 message
74+
$client->wait(1);
75+
76+
// Request Response
7577

7678
# Responding to requests
77-
$sid = $client->subscribe("sayhello", function ($res) {
78-
$res->reply("Hello, " . $res->getBody() . " !!!");
79+
$sid = $client->subscribe("sayhello", function ($message) {
80+
$message->reply("Reply: Hello, " . $message->getBody() . " !!!");
7981
});
8082

81-
# Wait for 1 message
82-
$client->wait(1);
83+
# Request
84+
$client->request('sayhello', 'Marty McFly', function ($message) {
85+
echo $message->getBody();
86+
});
8387
```
8488

8589
### Encoded Connections
@@ -90,30 +94,32 @@ $options = new \Nats\ConnectionOptions();
9094
$client = new \Nats\EncodedConnection($options, $encoder);
9195
$client->connect();
9296

93-
# Simple Publisher
94-
$client->publish("foo", array("one", "two"));
97+
// Publish Subscribe
9598

9699
# Simple Subscriber
97100
$callback = function($payload)
98101
{
99-
printf("Data: %s\r\n", var_dump($payload));
102+
printf("Data: %s\r\n", $payload->getBody()[1]);
100103
};
101104
$client->subscribe("foo", $callback);
102105

103-
# Request
104-
$client->request('sayhello', ['foo', 'Marty McFly'], function ($response) {
105-
echo 'Hello '.$response->getBody()[1].' !!!';
106-
});
106+
# Simple Publisher
107+
$client->publish("foo", ["foo", "bar"]);
107108

108-
# Responding to requests
109-
$sid = $client->subscribe("sayhello", function ($res) {
110-
$res->reply("Hello, " . $res->getBody() . " !!!");
111-
});
109+
# Wait for 1 message
110+
$client->wait(1);
112111

112+
// Request Response
113113

114+
# Responding to requests
115+
$sid = $client->subscribe("sayhello", function ($message) {
116+
$message->reply("Reply: Hello, " . $message->getBody()[1] . " !!!");
117+
});
114118

115-
# Wait for 1 message
116-
$client->wait(1);
119+
# Request
120+
$client->request('sayhello', ["foo", "McFly"], function ($message) {
121+
echo $message->getBody();
122+
});
117123
```
118124

119125

examples/pubsub/jsonencodedpubsub.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
// Publish Subscribe
1010

11-
1211
# Simple Subscriber
1312
$callback = function($payload)
1413
{
@@ -22,16 +21,3 @@
2221
# Wait for 1 message
2322
$client->wait(1);
2423

25-
// Request Response
26-
27-
28-
# Responding to requests
29-
$sid = $client->subscribe("sayhello", function ($message) {
30-
$message->reply("Reply: Hello, " . $message->getBody()[1] . " !!!");
31-
});
32-
33-
# Request
34-
$client->request('sayhello', ["foo", "McFly"], function ($message) {
35-
echo $message->getBody();
36-
});
37-

examples/pubsub/pubsub.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,3 @@
1818

1919
# Wait for 1 message
2020
$client->wait(1);
21-
22-
// Request Response
23-
24-
# Responding to requests
25-
$sid = $client->subscribe("sayhello", function ($message) {
26-
$message->reply("Reply: Hello, " . $message->getBody() . " !!!");
27-
});
28-
29-
# Request
30-
$client->request('sayhello', 'Marty McFly', function ($message) {
31-
echo $message->getBody();
32-
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
require_once __DIR__.'/../../vendor/autoload.php';
3+
4+
$encoder = new \Nats\Encoders\JSONEncoder();
5+
$options = new \Nats\ConnectionOptions();
6+
$client = new \Nats\EncodedConnection($options, $encoder);
7+
$client->connect();
8+
9+
// Request Response
10+
11+
# Responding to requests
12+
$sid = $client->subscribe("sayhello", function ($message) {
13+
$message->reply("Reply: Hello, " . $message->getBody()[1] . " !!!");
14+
});
15+
16+
# Request
17+
$client->request('sayhello', ["foo", "McFly"], function ($message) {
18+
echo $message->getBody();
19+
});

examples/reqres/req.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

examples/reqres/reqres.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once __DIR__.'/../../vendor/autoload.php';
3+
4+
$client = new \Nats\Connection();
5+
$client->connect();
6+
7+
// Request Response
8+
9+
# Responding to requests
10+
$sid = $client->subscribe("sayhello", function ($message) {
11+
$message->reply("Reply: Hello, " . $message->getBody() . " !!!");
12+
});
13+
14+
# Request
15+
$client->request('sayhello', 'Marty McFly', function ($message) {
16+
echo $message->getBody();
17+
});

examples/reqres/res.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)