| Home | Libraries | People | FAQ | More

## Examples

Source code and build scripts for these programs are located in the example directory.

### Clients

These HTTP clients submit a GET request to a server specified on the command line, and prints the resulting response. The crawl client asynchronously fetches the document root of the 10,000 top ranked domains, this may be used to evaluate robustness. All asynchronous clients support timeouts.

Description | Source File | Source File (using SSL)
--- | --- | ---
HTTP, synchronous | http_client_sync.cpp | http_client_sync_ssl.cpp
HTTP, asynchronous | http_client_async.cpp | http_client_async_ssl.cpp
HTTP, coroutine | http_client_coro.cpp | http_client_coro_ssl.cpp
HTTP crawl (asynchronous) | http_crawl.cpp |

These WebSocket clients connect to a server and send a message, then receive a message and print the response before disconnecting. All asynchronous clients support timeouts.

Description | Source File | Source File (using SSL)
--- | --- | ---
WebSocket, synchronous | websocket_client_sync.cpp | websocket_client_sync_ssl.cpp
WebSocket, asynchronous | websocket_client_async.cpp | websocket_client_async_ssl.cpp
WebSocket, coroutine | websocket_client_coro.cpp | websocket_client_coro_ssl.cpp

### Servers

These HTTP servers deliver files from a root directory specified on the command line. All asynchronous servers support timeouts.

Description | Source File | Source File (using SSL)
--- | --- | ---
HTTP, synchronous | http_server_sync.cpp | http_server_sync_ssl.cpp
HTTP, asynchronous | http_server_async.cpp | http_server_async_ssl.cpp
HTTP, coroutine | http_server_coro.cpp | http_server_coro_ssl.cpp
HTTP, stackless coroutine | http_server_stackless.cpp | http_server_stackless_ssl.cpp
HTTP, fast (optimized for speed) | http_server_fast.cpp |
HTTP, small (optimized for space) | http_server_small.cpp |
HTTP, flex (plain + SSL) |  | http_server_flex.cpp

These WebSocket servers echo back any message received, keeping the session open until the client disconnects. All asynchronous servers support timeouts.

Description | Source File | Source File (using SSL)
--- | --- | ---
WebSocket, synchronous | websocket_server_sync.cpp | websocket_server_sync_ssl.cpp
WebSocket, asynchronous | websocket_server_async.cpp | websocket_server_async_ssl.cpp
WebSocket, coroutine | websocket_server_coro.cpp | websocket_server_coro_ssl.cpp
WebSocket, stackless coroutine | websocket_server_stackless.cpp | websocket_server_stackless_ssl.cpp
WebSocket, fast (suited for benchmarks) | websocket_server_fast.cpp |

### Servers (Advanced)

These servers offer both HTTP and WebSocket services on the same port, and illustrate the implementation of advanced features.

Description | Features | Sources
--- | --- | ---
Advanced | Timeouts Multi-threaded HTTP pipelining Parser-oriented HTTP reading Dual protocols: HTTP and WebSocket Clean exit via SIGINT (CTRL+C) or SIGTERM (kill) | advanced_server.cpp
Advanced, flex (plain + SSL) | Timeouts Multi-threaded HTTP pipelining Parser-oriented HTTP reading Dual protocols: HTTP and WebSocket Flexible ports: plain and SSL on the same port Clean exit via SIGINT (CTRL+C) or SIGTERM (kill) | advanced_server_flex.cpp
Chat Server, multi-threaded | Multi-threaded Broadcasting Messages Multi-user Chat Server JavaScript Browser Client Parser-oriented HTTP reading Dual protocols: HTTP and WebSocket Clean exit via SIGINT (CTRL+C) or SIGTERM (kill) | chat-multi

### Chat Server 🎦

This example demonstrates a websocket chat server, allowing multiple users to connect and participate in live, group messaging. It comes with a tiny front end implemented in JavaScript and HTML5 which runs in any browser. The example is accompanied by a one hour presentation which provides a discussion of networking concepts, followed by in-depth explanation of how the client and server are constructed. This talk was delivered at CppCon 2018. The source code in the Beast example contains improvements to the original program.

Table 1.1. Chat WebSocket Server and JavaScript Client

Component | Features | Sources
--- | --- | ---
Server | C++ Timeouts Multi-threaded Broadcast to multiple peers Dual protocols: HTTP and WebSocket Clean exit via SIGINT (CTRL+C) or SIGTERM (kill) | chat-multi
Client | JavaScript / HTML5 Runs in the browser Delivered by the server Only 60 lines total including UI Completely portable graphics | chat_client.html