How to add WebSocket to Genie Site?

The Genie.jl documentation does not seem to have any information as to how to include a websocket in a web app, all the examples are just from the command line. I have a website composed of html and javascript in a webapp, but I cannot figure out how to add the Assets.channels_support() script such that I can use websockets with my existing html website. Currently, I try loading my website by routing with an html render function and I run Assets.channels_support(), but the latter appears to overwrite the former, but I want the html to still display. How can I do this?

I needed to do many months ago a small web application with a form and websockets (to avoid the reload of the page).

From this documentation, I needed to understand that Assets.channels_support() was just a string, so my main page was something like:

function show_page()
    form = read("public/form.html") |> String  # existing HTML page
    return Assets.channels_support() * form  # combining with channel support

Unfortunately this solution will not work for me since my webpage is spread across multiple html, javascript, and css files which reference each other by filename, so I cannot simply string them all together.

Do you have any suggestions on how to connect a WebSocket to an MVC web app @essenciary?

You put <% Genie.Assets.channels_support() %> inside the HTML of your web page and this outputs a <script>...</script> tag and sets up routing and everything else.

This in effect, in the browser, gives you access to a Genie.WebChannels object and you can call Genie.WebChannels.sendMessageTo(...) to push data over the websocket.

On the server, you define the corresponding channel(...) to catch/route/delegate these websockets requests to your handler functions.

1 Like

Thank you very much for your answer, that worked for me!

Related github issue here: Websocket Documentation Missing Website Integration · Issue #552 · GenieFramework/Genie.jl · GitHub

1 Like

Happy to hear it!