WalkPhoneGo

WalkPhoneGo

Can Phoenix channel detect client offline immediately?Like WiFi disconnected

I need to know whether the client is online, when WiFi is down.
The server should know it immediately. Can anyone tell me an elegant solution?

Now as I tested, Phoenix Channel needs about 40-50s to detect the client is disconnected.

Marked As Solved

chrismccord

chrismccord

Creator of Phoenix

You need to also set the webserver’s idle timeout to a lower value, ie

config :app, AppWeb.Endpoint, 
  http: [
    ..., 
    protocol_options: [
      idle_timeout: 10_000
    ]
  ]

Also Liked

NobbZ

NobbZ

An instant event in this case is technically impossible.

Only the client knows about the cut down WiFi. But since WiFi is down, it can’t tell the server.

There might even be situations in which neither of the participants know about the disconnect. Lets say your ISP drops the connection. Neither your client nor your server will know this until they try the next heartbeat and it times out.

This is just how networking works.

sb8244

sb8244

Author of Real-Time Phoenix

Hey @acrolink, I got your example figured out (feel free to post the example here, it may be useful for others?)

In the code example you sent, the idle_timeout was set at the cowboy HTTP config level. Run :ranch.get_protocol_options(MemeWeb.Endpoint.HTTP) to see what I mean. By default (no protocol options set), you won’t see idle_timeout in the HTTP endpoint. If you specify the idle timeout, then you’ll see it from that command.

However, cowboy_websocket uses a separate mechanism than the HTTP idle_timeout for it. You can see the option specified at https://ninenines.eu/docs/en/cowboy/2.2/manual/cowboy_websocket/. Phoenix sets up the cowboy_websocket opts at this line of code, and you have to specify it in the format timeout: X.

In your app, that is done with:

  socket("/socket", MemeWeb.UserSocket,
    websocket: [
      timeout: 10_000
    ]
  )

This code was commented out in the repo you sent me. I added it back in, set it to 10k, and I reliably detected failure within 10 seconds using your reproduction steps.

tl;dr the timeout option must be set in the socket options, and the UI must specify the heartbeatIntervalMs to a time lower than that. A value of 10_000 (socket) and 5_000 (client) will detect failure within 10 seconds.

WalkPhoneGo

WalkPhoneGo

Yes!!!, that’s what I need. work like a charm!!!!!!

Thank you!

I was fed up with the Actionable so I chose Phoenix Channel,it’s like magic!!!

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement