Websocket Client API

Overview

The Websocket client library allows Zephyr to connect to a Websocket server. The Websocket client API can be used directly by application to establish a Websocket connection to server, or it can be used as a transport for other network protocols like MQTT.

See this Websocket Wikipedia article for a detailed overview of how Websocket works.

For more information about the protocol itself, see IETF RFC6455 The WebSocket Protocol.

Websocket Transport

The Websocket API allows it to be used as a transport for other high level protocols like MQTT. The Zephyr MQTT client library can be configured to use Websocket transport by enabling CONFIG_MQTT_LIB_WEBSOCKET and CONFIG_WEBSOCKET_CLIENT Kconfig options.

First a socket needs to be created and connected to the Websocket server:

sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
...
ret = connect(sock, addr, addr_len);
...

The Websocket transport socket is then created like this:

ws_sock = websocket_connect(sock, &config, timeout, user_data);

The Websocket socket can then be used to send or receive data, and the Websocket client API will encapsulate the sent or received data to/from Websocket packet payload. Both the websocket_xxx() API or normal BSD socket API functions can be used to send and receive application data.

ret = websocket_send_msg(ws_sock, buf_to_send, buf_len,
                         WEBSOCKET_OPCODE_DATA_BINARY, true, true,
                         K_FOREVER);
...
ret = send(ws_sock, buf_to_send, buf_len, 0);

If normal BSD socket functions are used, then currently only TEXT data is supported. In order to send BINARY data, the websocket_send_msg() must be used.

When done, the Websocket transport socket must be closed.

ret = close(ws_sock);
or
ret = websocket_disconnect(ws_sock);

API Reference

group websocket

Websocket API.

Defines

WEBSOCKET_FLAG_FINAL

Message type values. Returned in websocket_recv_msg() Final frame

WEBSOCKET_FLAG_TEXT

Textual data

WEBSOCKET_FLAG_BINARY

Binary data

WEBSOCKET_FLAG_CLOSE

Closing connection

WEBSOCKET_FLAG_PING

Ping message

WEBSOCKET_FLAG_PONG

Pong message

Typedefs

typedef int (*websocket_connect_cb_t)(int ws_sock, struct http_request *req, void *user_data)

Callback called after Websocket connection is established.

Return

0 if ok, <0 if there is an error and connection should be aborted

Parameters
  • ws_sock: Websocket id

  • req: HTTP handshake request

  • user_data: A valid pointer on some user data or NULL

Enums

enum websocket_opcode

Values:

enumerator WEBSOCKET_OPCODE_CONTINUE
enumerator WEBSOCKET_OPCODE_DATA_TEXT
enumerator WEBSOCKET_OPCODE_DATA_BINARY
enumerator WEBSOCKET_OPCODE_CLOSE
enumerator WEBSOCKET_OPCODE_PING
enumerator WEBSOCKET_OPCODE_PONG

Functions

int websocket_connect(int http_sock, struct websocket_request *req, int32_t timeout, void *user_data)

Connect to a server that provides Websocket service. The callback is called after connection is established. The returned value is a new socket descriptor that can be used to send / receive data using the BSD socket API.

Return

Websocket id to be used when sending/receiving Websocket data.

Parameters
  • http_sock: Socket id to the server. Note that this socket is used to do HTTP handshakes etc. The actual Websocket connectivity is done via the returned websocket id. Note that the http_sock must not be closed after this function returns as it is used to deliver the Websocket packets to the Websocket server.

  • req: Websocket request. User should allocate and fill the request data.

  • timeout: Max timeout to wait for the connection. The timeout value is in milliseconds. Value SYS_FOREVER_MS means to wait forever.

  • user_data: User specified data that is passed to the callback.

int websocket_send_msg(int ws_sock, const uint8_t *payload, size_t payload_len, enum websocket_opcode opcode, bool mask, bool final, int32_t timeout)

Send websocket msg to peer.

The function will automatically add websocket header to the message.

Return

<0 if error, >=0 amount of bytes sent

Parameters
  • ws_sock: Websocket id returned by websocket_connect().

  • payload: Websocket data to send.

  • payload_len: Length of the data to be sent.

  • opcode: Operation code (text, binary, ping, pong, close)

  • mask: Mask the data, see RFC 6455 for details

  • final: Is this final message for this message send. If final == false, then the first message must have valid opcode and subsequent messages must have opcode WEBSOCKET_OPCODE_CONTINUE. If final == true and this is the only message, then opcode should have proper opcode (text or binary) set.

  • timeout: How long to try to send the message. The value is in milliseconds. Value SYS_FOREVER_MS means to wait forever.

int websocket_recv_msg(int ws_sock, uint8_t *buf, size_t buf_len, uint32_t *message_type, uint64_t *remaining, int32_t timeout)

Receive websocket msg from peer.

The function will automatically remove websocket header from the message.

Return

<0 if error, >=0 amount of bytes received

Parameters
  • ws_sock: Websocket id returned by websocket_connect().

  • buf: Buffer where websocket data is read.

  • buf_len: Length of the data buffer.

  • message_type: Type of the message.

  • remaining: How much there is data left in the message after this read.

  • timeout: How long to try to receive the message. The value is in milliseconds. Value SYS_FOREVER_MS means to wait forever.

int websocket_disconnect(int ws_sock)

Close websocket.

One must call websocket_connect() after this call to re-establish the connection.

Parameters

static inline void websocket_init(void)
struct websocket_request
#include <websocket.h>

Websocket client connection request. This contains all the data that is needed when doing a Websocket connection request.