I would suggest a place to start would be to dig deep into the Chilipeppr (CP) code, particularly the Serial Port JSON Server(SPJS). SPJS runs on many machines, including RasPI, is written in GO for speed and is web centric.
CP is Open Source and well documented. It was developed by reverse engineering the tinyG Serial interface and uses low level status messaging to optimize buffer fill as its flow control mechanism. It evolved to also work with other motion control sub-systems such as GRBl and the G2core Line Mode protocol.
I’d also suggest you have a look at these links
https://github.com/synthetos/TinyG/issues
https://github.com/synthetos/g2/issues
where developers like yourself probe issues with the developers.
Start there, this topic could head in many different directions .