sushant12
Understanding @behaviour
This particular file is a behaviour.
https://github.com/phoenixframework/phoenix/blob/v1.3.4/lib/phoenix/transports/serializer.ex#L1
And this is implementing it
Now my question is, Why do I even need to use a behaviour? If I were to do this for the second link ^^ it will still work.
defmodule Phoenix.Transports.V2.WebSocketSerializer do
@moduledoc false
# @behaviour Phoenix.Transports.Serializer
alias Phoenix.Socket.{Reply, Message, Broadcast}
@doc """
Translates a `Phoenix.Socket.Broadcast` into a `Phoenix.Socket.Message`.
"""
def fastlane!(%Broadcast{} = msg) do
data = Poison.encode_to_iodata!([nil, nil, msg.topic, msg.event, msg.payload])
{:socket_push, :text, data}
end
.
.
.
So what’s the point of using behaviour? The Phoenix.Transports.Serializer just defines functions without function body and leaves it to other module to implemente that function’s body.
What am I missing?
Marked As Solved
peerreynders
FYI:
Also Liked
Ankhers
You can think of it like a contract that is visibile to the compiler.
Right now the behaviour defines a single function fastlane!/1. What if, in the future, that behaviour changed and added a new function. You would now have fastlane!/1 and foo/2. The compiler will let you know that you have a module that does not fully comply with the contract that has been written.
hauleth
@behaviour is just compiler annotation that helps you when you miss some functions required by the caller, it doesn’t change anything else.
MrDoops
The @behaviour redirects the dependencies allowing for swappable implementations of the interface. This means the Phoenix framework application code can depend on the Serializer behaviour/interface/contract instead of the WebSocketSerializer directly. So if there were a situation where you need a special serializer, you wouldn’t need to fork Phoenix or wait for them to change it. We would just need to implement another Serializer and configure Phoenix to use it.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








