Flick.Socket.Serializer (flick v0.1.1)

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
})

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.