Skip to main content

Websockets

ntex supports WebSockets out of the box. The web::ws::start() function performs the websocket handshake, sends the handshake response and runs a websocket service until the connection is closed. A websocket service is a regular ntex Service that accepts ws::Frame and optionally returns a ws::Message response. The service state is a ws::WsSink, which can be used to send messages to the peer at any time. The handler that calls start() writes the response itself, so it returns ().

The following is an example of a simple websocket echo server:


A simple websocket echo server example is available in the examples directory.

An example chat server with the ability to chat over a websocket or TCP connection is available in websockets/chat directory