SimonDanisch / Bonito.jl

Serving JS to the browser
MIT License
204 stars 29 forks source link

Add utilities for extending connection for those who want to manage websocket server themselves #244

Open frankier opened 1 month ago

frankier commented 1 month ago

It would be nice to add utilities for extending connection for those who want to manage websocket server themselves. This may simply be a matter of making some existing stuff public, or there might be a way to create a smaller API surface.

One scenario for this is integrating with web frameworks such as Oxygen.jl. This is potentially a very nice combo where more conventional web development can be used for most of a website, but Bonito can be wheeled in when needed as a LiveView work-a-like. See https://github.com/OxygenFramework/Oxygen.jl/pull/212

What variation of that PR would be most stable with regards to planned changed in Bonito?

It does not look like the current internal methods in /src/connection/websocket.jl are stable. They will at least be changed by https://github.com/SimonDanisch/Bonito.jl/pull/215 .

SimonDanisch commented 1 month ago

You mean: https://simondanisch.github.io/Bonito.jl/stable/extending.html ?

frankier commented 1 month ago

Yes that is great and already strictly it is sufficient. However, what I mean additionally is that lot of the websocket management code can be reused by other implementations so that they have the same behaviour as Bonito.

I'll try and be a bit more concrete. Can I use save_read, save_write? (Should they be called safe_read and safe_write?)

Maybe the parts of https://github.com/SimonDanisch/Bonito.jl/blob/1a6083e84a660cc50e25bcd13349e3106cffcc7f/src/connection/websocket.jl not dependent on HTTP.Server could be factored out

e.g.

mutable struct WebSocketSession
    socket::Union{Nothing,WebSocket}
    lock::ReentrantLock
    session::Union{Nothing,Session}
end

mutable struct WebSocketConnection <: FrontendConnection
    server::Server
    client::WebSocketSession
end

And then all functionality possible is implemented from WebSocketSession, and anyone who wants can write their own wrapper type and fill in this missing parts.

Does this seem like a reasonable direction for me to draft a PR?

frankier commented 1 month ago

Okay well maybe I'll give it a go in case it helps make it clearer whether this is something which is wanted.

SimonDanisch commented 1 month ago

Yeah sorry I still wasn't sure exactly what you want to PR. In general more re-using sounds good ;)