Looking for Clarification on Genie.jl and WebSockets

I’ve read through the docs and tried the examples, and so far so good. Everything worked as advertised. However, when I tried to experiment further, I got really confused.

How do I use /____/subscribe and /____/unsubscribe?

The docs state that these two channels have been created for me, but they don’t tell me how to use them. Is there an example anywhere of how to use these?

I actually tried to look for the Julia code that defines these channels, but I couldn’t find the code while searching through the GenieFramework/Genie.jl.

Channel Naming Confusion

In the docs, there was a channel defined on the Julia side like this:

channel("/____/echo") do
  @info "Received: $(params(:payload))"
end

To send a message to it from the browser, one could do something like this:

Genie.WebChannels.sendMessageTo(
  '____', 'echo', 'Hello!'
)

However, suppose there were a channel defined like this on the Julia side:

channel("/____/foo/bar") do
  @info "foo/bar received: $(params(:payload))"
end

The JavaScript that was able to reach this channel was:

Genie.WebChannels.sendMessageTo(
  '____', 'foo/bar', 'Hello!'
)

This makes me think that ____ is special. It also confuses me when it comes to the definition of “channel”. Is ____ a channel or is /____/foo/bar a channel?

Cleaning Up Disconnected Clients

The docs say:

You should routinely unsubscribe_disconnected_clients() to free memory.

Is this a reasonable way to do that?

unsub = @task begin
    while true
        Genie.WebChannels.unsubscribe_disconnected_clients()
        sleep(30)
    end
end
schedule(unsub)

If anyone could provide even partial answers to these questions, I’d appreciate it.

I ended up finding answers to these questions on my own.

How do I use /____/subscribe and /___/unsubscribe?

  • You don’t really have to, because you get auto-subscribed to the default channel ____ on websocket-enabled pages anyway.
  • If you wanted to unsubscribe from the client-side, you’d say:
Genie.WebChannels.sendMessageTo('____', 'unsubscribe')

There isn’t really any reason to do this though.

Channel Naming Confusion

  • The Genie docs use the word “channel” in two related but distinct ways.
  • The first meaning of “channel” is a medium through which messages travel. It’s Genie’s abstraction on top of websockets.
  • The second meaning of “channel” is found on the server side when you use the channel function to create a channel route handler. The channel function does NOT create a channel. You’re setting up a route handler for Genie’s channel abstraction.

In those channel route handlers, there is an unspoken naming convention that must be followed. The first segment must be a channel name. The remaining segments are the route path.

Dissecting, channel("/____/foo/bar", ...):

  • ____ is the channel
  • foo/bar is the route path

More Observations on Channels

  • You don’t have to do anything special to create a channel.
  • It’s kind of like IRC where if you join a non-existent channel, it auto-vivifies.
  • This has to be done on the server-side.
  • Clients cannot create or join arbitrary channels.
  • All subscription/unsubscription is controlled on the server-side with code you write yourself.

Cleaning Up Disconnected Clients

The scheduled task from the previous post seems to work.

3 Likes