[zeromq-dev] Getting started with Websockets

Arnaud Loonstra arnaud at sphaero.org
Fri Jan 22 13:14:53 CET 2021


Hey all,

I'm currently getting started with the new websockets transport. It's 
not exposed in czmq but I managed to make it work.

However as it is websockets I'd like to get it to do something in a 
browser as well. I have this example running which works. Although I 
can't prove whether this is really websockets!

Help, tips, suggestions very welcome! I'm also going to open an issue on 
cmzq!

Rg,

Arnaud

// compile: gcc test-poll.c `pkg-config --cflags --libs libczmq`
#include "czmq.h"

int
main (int argc, char *argv [])
{
     // setup sockets
     zsock_t* zwss = zsock_new( ZMQ_PUSH );
     assert( zwss );
     //int rc = zsock_bind( zwss, "ws://127.0.0.1:1234/test" ); // 
doesn't work!
     int rc = zmq_bind(zsock_resolve( zwss ), "ws://127.0.0.1:1234/test");
     //int rc = zsock_bind( zwss, "tcp://127.0.0.1:1234" );
     assert( rc == 0 || rc == 1234 );

     zsock_t* zwsr = zsock_new( ZMQ_PULL );
     assert( zwsr );
     rc = zsock_connect( zwsr, "ws://127.0.0.1:1234/test");
     //zsock_set_subscribe(zwsr, "");
     //rc = zsock_connect( zwsr, "tcp://127.0.0.1:1234");
     assert( rc == 0);

     // setup poller
     zpoller_t *p = zpoller_new(zwsr, NULL);
     assert(p);

     zsys_info("starting");
     while (true)
     {
         ///  poll receiver for 1 sec
         void *which = zpoller_wait(p, 1000);
         if ( which != NULL)
         {
             zsys_info("receiving");
             assert(which == zwsr);
             zmsg_t *msg = zmsg_recv( zwsr );
             assert (msg);
             char *message;
             message = zmsg_popstr (msg);
             assert (streq(message, "hello websocket"));
             zsys_info("%s", message);
             zstr_free (&message);
             zmsg_destroy(&msg);
         }
         ///  send HELLO
         zsys_info("sending");
         int rc  = zstr_send( zwss, "hello websocket");
         assert(rc == 0);
         zclock_sleep(1000);
     }
     zsock_destroy(&zwss);
     zsock_destroy(&zwsr);
}


More information about the zeromq-dev mailing list