Skip to content

Commit

Permalink
docs: fix the pub/sub example in the doc (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanYuYuan authored Jan 7, 2025
1 parent 53d062c commit b8d1c9f
Showing 1 changed file with 56 additions and 53 deletions.
109 changes: 56 additions & 53 deletions docs/pubsub.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,77 @@
Publish / Subscribe
===================

The publish / subscribe pattern is implemented with classes :cpp:class:`zenoh::Publisher`
The publish / subscribe pattern is implemented with classes :cpp:class:`zenoh::Publisher`
and :cpp:class:`zenoh::Subscriber`.

Publisher example:

.. code-block:: c++

#include "zenoh.hxx"
using namespace zenoh;
#include "zenoh.hxx"

int main(int argc, char **argv) {
Config config = Config::create_default();
auto session = Session::open(std::move(config));
// Publish without creating a Publisher object
session.put(KeyExpr("demo/example/simple"), Bytes::serialize("Simple!"));
using namespace zenoh;

// Publish from a Publisher object
auto publisher = session.declare_publisher(KeyExpr("demo/example/simple"));
publisher.put("Simple!");
}
int main(int argc, char **argv) {
Config config = Config::create_default();
auto session = Session::open(std::move(config));
// Publish without creating a Publisher object
session.put(KeyExpr("demo/example/simple"), Bytes("Simple from session.put!"));
// Publish from a Publisher object
auto publisher = session.declare_publisher(KeyExpr("demo/example/simple"));
publisher.put("Simple from publisher.put!");
}

Subscriber example:

.. code-block:: c++

#include "zenoh.hxx"
#include <iostream>
using namespace zenoh;

int main(int argc, char **argv) {
Config config = Config::create_default();
auto session = Session::open(std::move(config));
auto subscriber = session.declare_subscriber(
KeyExpr("demo/example/simple"),
[](const Sample& sample) {
std::cout << "Received: " << sample.get_payload().deserialize<std::string>() << std::endl;
}
);
// Wait for a key press to exit
char c = getchar();
}
#include "zenoh.hxx"
#include <iostream>

using namespace zenoh;

int main(int argc, char **argv) {
Config config = Config::create_default();
auto session = Session::open(std::move(config));
auto subscriber = session.declare_subscriber(
KeyExpr("demo/example/simple"),
[](const Sample& sample) {
std::cout << "Received: " << sample.get_payload().as_string() << std::endl;
},
closures::none
);
// Wait for a key press to exit
char c = getchar();
}
Subscriber example with non-blocking stream interface:

.. code-block:: c++

#include "zenoh.hxx"
#include <iostream>
#include <thread>
#include <chrono>
using namespace zenoh;
using namespace std::chrono_literals;

int main(int argc, char **argv) {
Config config = Config::create_default();
auto session = Session::open(std::move(config));
auto subscriber = session.declare_subscriber(
KeyExpr("demo/example/simple"),
channels::FifoChannel(16), // use FIFO buffer to store unprocessed messages
);
while (true) {
auto res = subscriber.handler().try_recv()
if (std::holds_alternative<Sample>(res)) {
std::cout << "Received: " << std::get<Sample>(res).get_payload().deserialize<std::string>() << std::endl;
} else if (std::get<channels::RecvError>(res) == channels::RecvError::Z_NODATA) {
std::this_thread::sleep_for(1s); // do some other work
} else {
break; // channel is closed
}
}
}
#include "zenoh.hxx"
#include <iostream>
#include <thread>

using namespace zenoh;
using namespace std::chrono_literals;

int main(int argc, char **argv) {
Config config = Config::create_default();
auto session = Session::open(std::move(config));
auto subscriber = session.declare_subscriber(
KeyExpr("demo/example/simple"),
channels::FifoChannel(16) // use FIFO buffer to store unprocessed messages
);
while (true) {
auto res = subscriber.handler().try_recv();
if (std::holds_alternative<Sample>(res)) {
std::cout << "Received: " << std::get<Sample>(res).get_payload().as_string() << std::endl;
} else if (std::get<channels::RecvError>(res) == channels::RecvError::Z_NODATA) {
std::this_thread::sleep_for(1s); // do some other work
} else {
break; // channel is closed
}
}
}

0 comments on commit b8d1c9f

Please sign in to comment.