I implementated a concept like this with tcp but I took another approach (without the polymerphism) .
By simply using the pattern matching , I have a module Message where there is a function encode , decode, and process.
The encode takes a message (struct) and serializes it by prepending a message which is then used to pattern match on the decode function.
And then the process take message struct as parameter to perform the task for the command.
Example:
defmodule Message do
def encode(%MyMessage{}) do
<<0::8>>
end
def decode(<<0::8>>), do: %MyMessage{}
def process(%MyMessage{}) do
# Doing stuff
end
end
And on the tcp handler:
data_to_send = data_received
|> Message.decode()
|> Message.process()
|> Message.encode() # If you want to send a response
If it can help you ![]()


















