---
title: "Create a WebSocket Server"
---

> Documentation Index
> Fetch the complete documentation index at: https://openarkstudio.github.io/arknet/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a WebSocket Server

Configure a C++20 application using [Installation](/arknet/get-started/installation). Start the server before running the [matching client](/arknet/features/websocket/client).

## Binary Echo Server

WebSocket has its own message framing; do not pass `use_dgram` or a TCP delimiter.
The server preserves the received message's text/binary type when echoing it.
Build this server and the [client](/arknet/features/websocket/client) as separate executables using the `arknet::arknet` interface
target. Run the server first, then the client in another terminal. Press Enter in
the server terminal to stop it.

```cpp
#include <iostream>
#include <string_view>

#include <arknet/websocket/ws_server.hpp>

int main()
{
    arknet::ws_server server;
    server.bind_recv([](auto& session, std::string_view bytes)
    {
        session->ws_stream().binary(session->ws_stream().got_binary());
        session->async_send(bytes);
    });
    if (!server.start("127.0.0.1", 7001))
    {
        std::cerr << arknet::get_last_error().message() << '\n';
        return 1;
    }
    std::cout << "WebSocket listening on ws://127.0.0.1:7001/echo\n";
    std::cin.get();
    server.stop();
}
```


[Configuration and advanced usage](/arknet/features/websocket/configuration) · [Performance](/arknet/features/websocket/performance)

Source: https://openarkstudio.github.io/arknet/features/websocket/server/index.mdx
