1
0
mirror of https://github.com/taigrr/nats.docs synced 2025-01-18 04:03:23 -08:00

Add C Client examples

This commit is contained in:
ainsley
2020-08-26 09:29:26 -05:00
parent 2c9b2f91fc
commit 4382b309d9
34 changed files with 1165 additions and 0 deletions

View File

@@ -109,6 +109,29 @@ await nc.flush();
nc.close();
```
{% endtab %}
{% tab title="C" %}
```c
natsConnection *conn = NULL;
natsStatus s = NATS_OK;
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
// Send a request and wait for up to 1 second
if (s == NATS_OK)
s = natsConnection_PublishString(conn, "foo", "All is Well");
// Sends a PING and wait for a PONG from the server, up to the given timeout.
// This gives guarantee that the server has processed the above message.
if (s == NATS_OK)
s = natsConnection_FlushTimeout(conn, 1000);
(...)
// Destroy objects that were created
natsConnection_Destroy(conn);
```
{% endtab %}
{% endtabs %}
## Flush and Ping/Pong

View File

@@ -155,5 +155,23 @@ await nc.subscribe(inbox, (err, msg) => {
nc.publish('time', "", inbox);
```
{% endtab %}
{% tab title="C" %}
```c
natsConnection *conn = NULL;
natsStatus s = NATS_OK;
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
// Publish a message and provide a reply subject
if (s == NATS_OK)
s = natsConnection_PublishRequestString(conn, "request", "reply", "this is the request");
(...)
// Destroy objects that were created
natsConnection_Destroy(conn);
```
{% endtab %}
{% endtabs %}

View File

@@ -110,6 +110,36 @@ t.log('the time is', msg.data);
nc.close();
```
{% endtab %}
{% tab title="C" %}
```c
natsConnection *conn = NULL;
natsMsg *msg = NULL;
natsStatus s = NATS_OK;
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
// Send a request and wait for up to 1 second
if (s == NATS_OK)
s = natsConnection_RequestString(&msg, conn, "request", "this is the request", 1000);
if (s == NATS_OK)
{
printf("Received msg: %s - %.*s\n",
natsMsg_GetSubject(msg),
natsMsg_GetDataLength(msg),
natsMsg_GetData(msg));
// Destroy the message that was received
natsMsg_Destroy(msg);
}
(...)
// Destroy objects that were created
natsConnection_Destroy(conn);
```
{% endtab %}
{% endtabs %}
You can think of request-reply in the library as a subscribe, get one message, unsubscribe pattern. In Go this might look something like:

View File

@@ -106,5 +106,11 @@ let nc = await connect({
nc.publish('updates', {ticker: 'GOOG', price: 1200});
```
{% endtab %}
{% tab title="C" %}
```c
// Structured data is not configurable in C NATS Client.
```
{% endtab %}
{% endtabs %}