onMsg(natsConnection *conn, natsSubscription *sub, natsMsg *msg, void *closure)
printf("Received msg: %s - %.*s\n",
natsMsg_GetDataLength(msg),
// Add some delay while processing
// Need to destroy the message!
closeHandler(natsConnection *conn, void *closure)
cond_variable cv = (cond_variable) closure;
notify_cond_variable(cv);
natsConnection *conn = NULL;
natsOptions *opts = NULL;
natsSubscription *sub = NULL;
cond_variable cv = new_cond_variable(); // some fictuous way to notify between threads.
s = natsOptions_Create(&opts);
// Setup a close handler and pass a reference to our condition variable.
s = natsOptions_SetClosedCB(opts, closeHandler, (void*) cv);
s = natsConnection_Connect(&conn, opts);
s = natsConnection_Subscribe(&sub, conn, "foo", onMsg, NULL);
s = natsConnection_PublishString(conn, "foo", "hello");
// Drain the connection, which will close it when done.
s = natsConnection_Drain(conn);
// Wait for the connection to be closed
// Destroy objects that were created
natsSubscription_Destroy(sub);
natsConnection_Destroy(conn);
natsOptions_Destroy(opts);