Flick. Socket. Serializer
(flick v0.2.0)
Copy Markdown
A Phoenix.Socket.Serializer that encodes the whole Channels envelope
(join_ref, ref, topic, event, payload) as a single Erlang
External Term Format (ETF) binary, paired with flick.js's channel
serializer on the client.
Unlike Phoenix.Socket.V2.JSONSerializer, every message — regardless of
payload shape — is sent as a single binary WebSocket frame containing
:erlang.term_to_binary([join_ref, ref, topic, event, payload]).
Usage
Configure your Phoenix.Socket to use this serializer instead of the
default JSON one:
transport :websocket, serializer: [{Flick.Socket.Serializer, "~> 2.0.0"}]On the client, use Flick.ChannelSerializer (vendored alongside
flick.js) when constructing the socket:
import {Socket} from "phoenix"
import FlickChannelSerializer from "./flick-channel"
const socket = new Socket("/socket", {
encode: FlickChannelSerializer.encode,
decode: FlickChannelSerializer.decode
})Decoding safety
Incoming binaries are decoded with :erlang.binary_to_term/2 using the
:safe option, which rejects atom creation and executable terms (funs).
If the optional :plug_crypto dependency is present, decode!/2 instead
calls Plug.Crypto.non_executable_binary_to_term/2 (also with :safe),
which guarantees rejection of executable terms regardless of OTP version.
mix flick.install requires {:plug_crypto, "~> 1.2 or ~> 2.0"} in your
deps by default; pass --no-plug-crypto to opt out.
Caveats
join_ref, ref, topic, and event are always coerced to/from
Elixir strings (binaries decode from/encode to JS strings on the wire).
The payload, however, is encoded/decoded as-is by flick.js/:erlang
— e.g. JS strings inside a payload decode to Erlang charlists (and
Erlang binaries decode to ErlBinary on the client), the same caveats
that apply to the raw WebSocket integration described in the README.