Choosing backend based on tcp payload in Lua

My message is a custom length prefixed (in binary).
I want to read three different data from TCP payload,
First the length then message type then based on message type I should find Client ID and choose proper backend(different message type has different structure)
I have read Choosing backend based on tcp payload but I need to do it in Lua because I need to read SQLite table or call other API to choose a backend based on ClientID.
In my last project I did the same in Erlang/OTP/ETS (I’ve created my own proxy)is it possible to do the same in HAProxy/Lua/Sqlite?