2

I want to use NATS to allow communication between a client and a server, but I want to use a public proxy server for the connection. However, it seems that the NATS API does not provide an option to configure proxy information. Is there any solution for this? Thanks.

Here is my client code:

`#include <iostream>
#include <nats/nats.h>

int main(int argc, char **argv)
{
    natsConnection      *conn = NULL;
    natsSubscription    *sub  = NULL;
    natsMsg             *msg  = NULL;
    natsOptions         *opts = NULL;
    natsStatus          s;

    printf("Listening on subject 'foo'\n");

    s = natsOptions_Create(&opts);
    s = natsOptions_SetURL(opts, "url");
    if (s != NATS_OK) {
        printf("set URL error");
        return -1;
    }

    s = natsConnection_Connect(&conn, opts);
    if (s != NATS_OK) {
        printf("connect error\n");
        return -1;
    }
    if (s == NATS_OK)
    {
        // Creates a synchronous subscription on subject "foo".
        s = natsConnection_SubscribeSync(&sub, conn, "foo");
    }
    if (s != NATS_OK) {
        printf("subscribe error\n");
        return -1;
    }

    while (true) {
        // With synchronous subscriptions, one needs to poll
        // using this function. A timeout is used to instruct
        // how long we are willing to wait. The wait is in milliseconds.
        // So here, we are going to wait for 5 seconds.
        s = natsSubscription_NextMsg(&msg, sub, 5000);
        if (s == NATS_OK)
        {
            // If we are here, we should have received a message.
            printf("Received msg: %s - %.*s\n",
                   natsMsg_GetSubject(msg),
                   natsMsg_GetDataLength(msg),
                   natsMsg_GetData(msg));

            // Need to destroy the message!
            natsMsg_Destroy(msg);
        }
    }

    // Anything that is created needs to be destroyed
    natsSubscription_Destroy(sub);
    natsConnection_Destroy(conn);

    // If there was an error, print a stack trace and exit
    if (s != NATS_OK)
    {
        nats_PrintLastErrorStack(stderr);
        exit(2);
    }

    return 0;
}`

I have tried setting up an nginx server as a stream proxy and it was successful. However, since NATS messages do not use the HTTP protocol, I am unsure how to use a public proxy server for NATS.

marslin
  • 21
  • 1

0 Answers0