hecate_plugin_ws (hecate_sdk v0.6.2)

View Source

WebSocket helper for plugin live UIs.

Provides a base behaviour for plugin WebSocket handlers. Handles JSON encoding/decoding and standard message framing.

Usage in a plugin: -module(my_plugin_ws). -behaviour(cowboy_websocket). -export([init/2, websocket_init/1, websocket_handle/2, websocket_info/2]).

init(Req, State) -> hecate_plugin_ws:upgrade(Req, State).

websocket_handle({text, Data}, State) -> Msg = hecate_plugin_ws:decode(Data), %% handle Msg ... {reply, hecate_plugin_ws:encode(Response), State}.

Summary

Functions

Decode a JSON binary into a map.

Encode a term as a JSON text frame.

Send a JSON error reply.

Send a JSON reply with a type tag.

Upgrade an HTTP request to WebSocket.

Functions

decode(Data)

-spec decode(binary()) -> map().

Decode a JSON binary into a map.

encode(Term)

-spec encode(term()) -> {text, iodata()}.

Encode a term as a JSON text frame.

reply_error(Code, Message, Details)

-spec reply_error(Code :: binary(), Message :: binary(), Details :: map()) -> {text, iodata()}.

Send a JSON error reply.

reply_json(Type, Payload)

-spec reply_json(Type :: binary(), Payload :: map()) -> {text, iodata()}.

Send a JSON reply with a type tag.

upgrade(Req, State)

-spec upgrade(cowboy_req:req(), term()) -> {cowboy_websocket, cowboy_req:req(), term()}.

Upgrade an HTTP request to WebSocket.