Phoenix.Socket.V2.BERTSerializer (phoenix_socket_bert v2.0.0)

Copy Markdown View Source

A serializer for Phoenix.Socket that encodes server-to-client messages using BERT (Binary ERlang Term)

Client-to-server messages use Phoenix's JSON and binary frame formats.

Server payloads may contain floats, JavaScript-safe integers, UTF-8 binaries, small UTF-8 atoms, proper lists, byte lists, small tuples, and maps. Binary replies and other Erlang terms are not supported.

Usage

Set the :serializer option in your Phoenix.Socket configuration:

endpoint.ex

socket "/live", Phoenix.LiveView.Socket,
  websocket: [
    connect_info: [session: @session_options],
    serializer: [{Phoenix.Socket.V2.BERTSerializer, "~> 2.0.0"}]
  ]

socket "/socket", Phoenix.Socket,
  websocket: [
    serializer: [{Phoenix.Socket.V2.BERTSerializer, "~> 2.0.0"}]
  ]

Import the phoenix_socket_bert in your app.js and add decode option to the LiveSocket configuration:

app.js

import { decode } from 'phoenix_socket_bert'
import { LiveSocket } from 'phoenix_live_view'
import { Socket } from 'phoenix'

let csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content')
let liveSocket = new LiveSocket('/live', Socket, {
  decode: decode,
  params: { _csrf_token: csrfToken }
})

liveSocket.connect()

let socket = new Socket("/socket", { decode: decode, params: { token: csrfToken } })
socket.connect()