1+ <?php
2+
3+ use Pulsar \Compression \Compression ;
4+ use Pulsar \MessageOptions ;
5+ use Pulsar \Producer ;
6+ use Pulsar \ProducerOptions ;
7+
8+ require_once __DIR__ . '/../vendor/autoload.php ' ;
9+
10+ $ options = new ProducerOptions ();
11+
12+ // If permission authentication is available
13+ // Only JWT authentication is currently supported
14+ // $options->setAuthentication(new Jwt('token'));
15+
16+ $ options ->setConnectTimeout (3 );
17+ $ options ->setTopic ('persistent://public/default/demo ' );
18+ $ options ->setCompression (Compression::ZLIB );
19+ $ producer = new Producer ('pulsar://localhost:6650 ' , $ options );
20+ $ producer ->connect ();
21+
22+ for ($ i = 0 ; $ i < 10 ; $ i ++) {
23+ $ messageID = $ producer ->send (sprintf ('hello %d ' , $ i ));
24+ echo 'messageID ' . $ messageID . "\n" ;
25+ }
26+
27+ // Sending messages asynchronously
28+ for ($ i = 0 ; $ i < 10 ; $ i ++) {
29+ $ producer ->sendAsync (sprintf ('hello-async %d ' , $ i ), function (string $ messageID ) {
30+ echo 'messageID ' . $ messageID . "\n" ;
31+ });
32+ }
33+ // Add this line when sending asynchronously
34+ $ producer ->wait ();
35+
36+ // Sending delayed messages
37+ for ($ i = 0 ; $ i < 10 ; $ i ++) {
38+ $ producer ->send (sprintf ('hello-delay %d ' , $ i ), [
39+ MessageOptions::DELAY_SECONDS => $ i * 5 , // Seconds
40+ ]);
41+ }
42+
43+ // close
44+ $ producer ->close ();
0 commit comments