https://github.com/young-flash/websockets-demo
This is a demo about how to use uWebsockets to implement a server which supports publish and subscribe over websockets protocol (with ssl).
https://github.com/young-flash/websockets-demo
cpp publish-subscribe websocket websockets-server
Last synced: about 1 year ago
JSON representation
This is a demo about how to use uWebsockets to implement a server which supports publish and subscribe over websockets protocol (with ssl).
- Host: GitHub
- URL: https://github.com/young-flash/websockets-demo
- Owner: Young-Flash
- Created: 2022-03-27T12:30:07.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-04-04T08:50:20.000Z (about 4 years ago)
- Last Synced: 2025-02-09T00:28:53.431Z (over 1 year ago)
- Topics: cpp, publish-subscribe, websocket, websockets-server
- Language: C++
- Homepage:
- Size: 112 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Overview
This is a demo about how to use [uWebsockets](https://github.com/uNetworking/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.
# Setup
You must make [uWebsockets](https://github.com/uNetworking/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](https://flash-flash.gitee.io/2022/03/27/uWebsockets%20%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85/) if you have problem on compiling and
install uWebsockets and uSockets.
# Build
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`
# Run
You can use Postman as client to access the websockets server
|  |  |
|---------------------------------------------------|----------------------------------------------------|