- Notifications
You must be signed in to change notification settings - Fork2
TCP transport layer for Phoenix
License
WallarooLabs/phoenix_tcp
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
POC TCP Transport for the Phoenix Framework
Ifavailable in Hex, the package can be installed as:
Add phoenix_tcp to your list of dependencies in
mix.exs
:def deps do[{:phoenix_tcp, "~> 0.0.1"}][{:phoenix_tcp, git: "git@github.com:sendence/phoenix_tcp.git", tag: "v0.0.1"}]end
In your application file add the following as a child after your
endpoint
:supervisor(PhoenixTCP.Supervisor, [:app_name, Chat.Endpoint])
In your config.ex add to your endpoints config:
tcp_handler: PhoenixTCP.RanchHandler, tcp: [port: System.get_env("PHX_TCP_PORT") || 5001]
In your socket(s) add the following:
transport :tcp, PhoenixTCP.Transports.TCP
in order to send data to Phoenix over TCP using this transport, the data must be sent in binary format. The first 4 bytes will be used to determine the message size and the remaining bytes will be the message.Ex. using Elixir:
iex> opts = [:binary, active: false][:binary, {:active, false}]iex> {:ok, socket} = :gen_tcp.connect('localhost', 5001, opts){:ok, #Port<0.5652>}iex> path = "{\"path\": \"/socket/tcp\", \"params\": {}}""{\"path\": \"/socket/tcp\", \"params\": {}}"iex> path_msg = << byte_size(path) :: size(32) >> <> path<<0, 0, 0, 37, 123, ... 125, 125>>iex> :tcp_send(socket, path_msg)...
The server initially expects to receive the following message in json:{"path": "/:path", "params": {}}
once a connection is established, the standard Phoenix join event is expected (a ref must be passed):{"event": "phx_join", "topic": "topic-name", "payload": null, "ref": null}
and subsequent messages are sent after the join is established in the same structure:{"event": ..., "topic": ..., "payload": ..., "ref": ...}