Presenting Boost.Beast: A NEW WebSocket Library that uses Boost.Asio, header-only and C++11 !!

1,701 views
Skip to first unread message

Vinnie Falco

unread,
Jan 5, 2018, 11:51:00 AM1/5/18
to WebSocket++
Have you ever thought to yourself, "I wish there was a WebSocket library that followed the Universal Asynchronous Model introduced in Boost.Asio and formalized into the Networking Technical Specification (http://cplusplus.github.io/networking-ts/draft.pdf)?" Have you ever desired to use traditional completion handlers, stackful or stackless coroutines, or std::future in your asynchronous WebSocket server? Have you ever wanted more control over how messages are represented or where the memory comes from? Well now you can have it!

Boost.Beast is a shiny new WebSocket library that is built on top of Boost.Asio, header-only, and written in pure C++11. It was designed from the ground up to resemble Asio and use all of the idioms and concepts that Asio users are accustomed to. The interfaces in this library are crisp, clean, intuitive, and well-defined. If you are familiar with Asio then you will be quite at home using Beast.

The beast::websocket::stream wraps your existing boost::asio::ip::tcp::socket, or boost::asio::ssl::stream and allows WebSocket communication in either client or server modes (the interface is symmetric). There are both synchronous and asynchronous interfaces, and members that either throw or return an error code. Beast asynchronous functions use "completion tokens" which means you can use a regular completion handler, coroutine, std::future, or even user-defined types to receive notifications when asynchronous operations complete.

Beast's implementation uses efficient "composed operations" which are optimized for using Asio's custom allocators and take advantage of C++11 move operators for performance.

WebSocket streams support the permessage-deflate extension, and Beast comes with a header-only, C++11 version of zlib so you don't even need to add it as a separate library!

As if this wasn't enough, Beast also comes with a complete HTTP library! You can model HTTP messages, send and receive requests and responses either synchronously or asynchronously. The built-in parser is fast and robust. Beast WebSockets integrate seamlessly with the HTTP algorithms, you can accept an HTTP connection, process some requests and responses, and pass the WebSocket upgrade back to Beast to upgrade your connection to WebSocket. You can also get control of the handshaking process to do anything you'd like, for example Basic Authentication or processing custom headers.

Boost.Beast is free, open source software that is part of Boost and available in Boost versions 1.66.0 and later. Anyone who is already using Boost.Asio directly or indirectly will find switching over to Boost.Beast to be quite simple!

The official repository for Beast is here:
https://github.com/boostorg/beast

Example programs:
http://www.boost.org/doc/libs/1_66_0/libs/beast/doc/html/beast/examples.html

HTML Documentation:
http://www.boost.org/doc/libs/1_66_0/libs/beast/doc/html/index.html

Security Review and Autobahn Test Results
http://www.boost.org/doc/libs/1_66_0/libs/beast/doc/html/beast/reports.html#beast.reports.security_review_bishop_fox

Thanks!

-- 
Follow me on Github: https://github.com/vinniefalco

F. Eugene Aumson

unread,
May 31, 2018, 2:34:20 PM5/31/18
to WebSocket++
Given the forum here, can you compare and contrast with websocketpp?

Vinnie Falco

unread,
May 31, 2018, 3:24:35 PM5/31/18
to WebSocket++
On Thursday, May 31, 2018 at 11:34:20 AM UTC-7, F. Eugene Aumson wrote:
Given the forum here, can you compare and contrast with websocketpp?

Yep, glad to. Beast is designed to use the established Asio idioms to the greatest extent possible. It lets users adopt the buffer sequence, dynamic buffer, and stream abstractions found in Asio for flexibility. Beast leaves important decisions to the user such as how memory for buffers is allocated or how the connections are established (as client or server). It doesn't impose any particular structure, so you can design your program in the way that is best for your use-case.

Here is the documentation page with a more technical comparison of Beast and websocketpp:

<https://www.boost.org/doc/libs/1_67_0/libs/beast/doc/html/beast/design_choices/comparison_to_zaphoyd_studios_we.html>

Beast also compares favorably to other libraries:

<https://www.boost.org/doc/libs/1_67_0/libs/beast/doc/html/beast/design_choices/http_comparison_to_other_librari.html>

Beast is already part of Boost so if you're using websocketpp then you are already using Boost. You just need to upgrade to Boost version 1.66 or later and you will have Beast.

Another really cool thing about Beast is that it supports the permessage-deflate extension without requiring linking to ZLib. And yet Beast also remains header-only. You can even turn off permessage-deflate support at compile time using a template parameter constant, so that the resulting compiled code is smaller.

And finally, Beast has a very robust HTTP message model. You get full, high-level control over the WebSocket handshake. And you get powerful algorithms for sending and receiving HTTP messages. 

For example, here's an asynchronous HTTP server that supports a WebSocket echo application. This server operates both plain and SSL on the same port using the SSL handshake detector tutorial in the Beast documentation. It also serves files from an HTTP document root directory:

<https://github.com/boostorg/beast/blob/34e78cb9004f9216ca389823c1118403d73c596d/example/advanced/server-flex/advanced_server_flex.cpp>

Here's a WebSocket chatroom server example with a Javascript client, so you can get a feel of how Beast WebSocket applications are designed. This will be the subject of my 1 hour talk at CppCon 2018 in September:

<https://github.com/vinniefalco/CppCon2018>

Hope this helps!

Reply all
Reply to author
Forward
0 new messages