# Websockets - Crow
A Fast and Easy to use microframework for the web.

# Websockets

Websockets are a way of connecting a client and a server without the request response nature of HTTP.

To create a websocket in Crow, you need a websocket route.
A websocket route differs from a normal route quite a bit. While it uses the same `CROW_ROUTE(app, "/url")` macro, that's about where the similarities end.
A websocket route follows the macro with `.websocket()` which is then followed by a series of methods (with handlers inside) for each event. These are (sorted by order of execution):

Warning

By default, Crow allows Clients to send unmasked websocket messages, which is useful for debugging but goes against the protocol specification. Production Crow applications should enforce the protocol by adding `#define CROW_ENFORCE_WS_SPEC` to their source code.

- `onaccept([&](const crow::request&){handler code goes here})` (This handler has to return `bool`)
- `onopen([&](crow::websocket::connection& conn){handler code goes here})`
- `onmessage([&](crow::websocket::connection& conn, const std::string message, bool is_binary){handler code goes here})`
- `onerror([&](crow::websocket::connection& conn){handler code goes here})`
- `onclose([&](crow::websocket::connection& conn, const std::string reason){handler code goes here})`

These event methods and their handlers can be chained. The full Route should look similar to this:

    CROW_ROUTE(app, "/ws")
        .websocket()
        .onopen([&](crow::websocket::connection& conn){
                do_something();
                })
        .onclose([&](crow::websocket::connection& conn, const std::string& reason){
                do_something();
                })
        .onmessage([&](crow::websocket::connection& /*conn*/, const std::string& data, bool is_binary){
                    if (is_binary)
                        do_something(data);
                    else
                        do_something_else(data);
                });

For more info go here.