This is a demo about how to use uWebsockets to implement a server which supports publish and subscribe over websockets protocol (with ssl).
There are two threads, the main thread listen on ip:port
to wait websockets client to connect and subscribe some
topic
which indicated in the message it sent to server, another thread (publishTopicRandomly
) simulate topic publish, if a topic which have been subscribed by a specific client published, the message related to this topic will be sent to the corresponding client. mutex and condition_variable are used to synchronize the two threads, thread publishTopicRandomly
publish topic randomly after there are some clients subscribe.
You must make uWebsockets and its dependency(uSockets, openssl, zlib,
etc) ready in your dev machine. Noted that wss (websockets with ssl) are used here so you have to compile
uWebsockets with ssl option (WITH_OPENSSL=1 make
). You can take a look at my blog if you have problem on compiling and
install uWebsockets and uSockets.
This demo can build with CMake easily
git clone https://github.com/Young-Flash/websockets-demo.git
cd websocket-demo
mkdir build && cd build
cmake ..
make
./websocket-demo
You can use Postman as client to access the websockets server