Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drab Manually publish to socket #20

Closed
JanStevens opened this issue Jun 14, 2017 · 22 comments
Closed

Drab Manually publish to socket #20

JanStevens opened this issue Jun 14, 2017 · 22 comments

Comments

@JanStevens
Copy link

Is it possible to have a separate gen server that then triggers an update for all connected clients using drab?

I have a GenServer that polls for data / transforms and then stores the results, what I want is to show a live feed of the resulting data whenever it changes in all the connected browsers.

What I had now is a recursive loop with a sleep in the drab method, ex:

  defp table_loop(socket) do
    :timer.sleep(2000)
    data =    ExpensiveData.get_current
    socket |> update!(:html, set: "<p>data<p>", on: ".table")
    table_loop(socket)
  end

It works but I wonder if there is a better way to do it. I would rather setup a GenEvent and let the GenServer send a notification that the data has changed so I can display it. This means I don't have to continually poll for updates like I do now in the above function.

Regards

@grych
Copy link
Owner

grych commented Jun 14, 2017

This is a known problem. I have it in my TODO list, but I still couldn't find a way how to resolve this.

The issue is that you need a Drab Server, which launches when the socket is created - on the client connect. After disconnect, the Drab Server and the socket is gone. What we need, is a kind of permanent socket and Drab, which we could use for a background broadcasting.

I've faced the similar issue while creating the presence demo. When user disconnects, there is no socket anymore, so I can't just broadcast a message to the others. I made an ugly hack there: got a random, connected socket and use it to broadcast.

I will keep this issue opened until I find the way to solve it correctly.

@grych
Copy link
Owner

grych commented Jul 13, 2017

This issue is solved with 0.5.0 (coming soon). Now, with broadcasting functions, you don't have to use the socket, but the subject, which doesn't have to be related to the socket.

@grych
Copy link
Owner

grych commented Jul 17, 2017

Fixed in v0.5.0

@grych grych closed this as completed Jul 17, 2017
@JanStevens
Copy link
Author

Hello,

Thanks looks awesome, just a question, I don't see in the guide / API any reference to the subject. So I'm not really sure how I should use it with poke and peek

@grych
Copy link
Owner

grych commented Jul 18, 2017

True, documentation is crappy in this topic. I am going to update it soon. Thanks for pointing it!

In general, there are subject creating functions: same_topic, same_controller and same_path. Use it instead of socket in broadcasting functions, example:

broadcast_js same_controller(MyApp.MyController), "console.log('bcast')"

it corresponds to controllers with

broadcasting :same_controller

@JanStevens
Copy link
Author

And I can use this in a pure GenServer how? I should use Drab.Core.same_controller or should I use Drab.Commander first before it would work (even when its not really a commander).

@grych
Copy link
Owner

grych commented Jul 18, 2017

You need to have browsers connected with Drab first, so you need commander(s). Drab broadcast works with browsers connected with Drab only. Then you can broadcast from any part of code

@johannesE
Copy link

I am trying to do what you are saying and following the documentation but getting the following error:
image
You can see, that it's using the same path /items, meaning the browser is connected, but I'm probably missing something obvious.

@grych grych reopened this Aug 19, 2017
@grych
Copy link
Owner

grych commented Aug 19, 2017

Unfortunatelly, I can't reproduce it. Can you please mention the phoenix and elixir version?

@grych
Copy link
Owner

grych commented Aug 19, 2017

@johannesE, could you please try to get https://github.com/grych/drab-example and run:

Drab.Core.broadcast_js Drab.Core.same_path("/"), "alert('a')"

on that?
So we would know if there is an issue with the environment or in the setup.

@johannesE
Copy link

When I run the example, I get [error] The client's requested channel transport version "2.0.0" does not match server's version requirements of "~> 1.0" every few seconds.
I use elixir 1.5.1 and used phx 1.3.0 in my app.

@grych
Copy link
Owner

grych commented Aug 19, 2017 via email

@johannesE
Copy link

Ah, but the broadcast is working in the example app.

The browser is chromium Version 60.0.3112.78 (Developer Build) Built on Ubuntu , running on Ubuntu 17.04 (64-bit)

@johannesE
Copy link

image

@johannesE
Copy link

FYI: I did not set up If you want to use Drab.Query (jQuery based module) in my project. Everything except the broadcast is working though.

@grych
Copy link
Owner

grych commented Aug 19, 2017

  1. About the broadcasting - if it works the example, it looks like it must be some misconfig. Is your code available somewhere?

  2. About the protocol version error: are you using Phoenix 1.3.0 or some RC? I can see your error were changed in this commit. Please upgrade to official 1.3.0

EDIT: drab-example uses Phoenix 1.2, so forget about point 2. I will need to reproduce the issue somehow.

BTW Query is not required. Actually, drab-example does not use it.

@johannesE
Copy link

My code is here: https://github.com/octotreat/todomvc. I was trying to build something like this: http://todomvc.com/.
Mix.lock is showing that I'm using "phoenix": {:hex, :phoenix, "1.3.0",

The transport version error only happened in the example application. In there I was using phoenix 1.2.4, like you. The only difference is the elixir version (1.5.1 instead of 1.4), so I'm guessing that is the problem there. It will probably go away once you upgrade the example app to phoenix 1.3.0. So I think we should not focus on that one now.

@grych
Copy link
Owner

grych commented Aug 19, 2017

Thx! I managed to reproduce this issue with fresh Phoenix 1.3 installation. Interesting that it does not appear when running 1.3 over application built in 1.2.

@johannesE
Copy link

Cool. :-)
It cannot appear then because the installation of phoenix on your machine only changes the mix phx.new generator. Afterwards it's using the phoenix version specified in mix.lock, not the global installation.

@grych
Copy link
Owner

grych commented Aug 19, 2017

Actually the issue is because in P13 the endpoint is MyAppWeb.Endpoint, and pubsub is moved to MyApp.Endpoint. Drab is a hack over Phoenix, so it is looking for a pubsub module in the wrong place.

@grych grych closed this as completed in 8da7de2 Aug 19, 2017
@grych grych reopened this Aug 19, 2017
@grych
Copy link
Owner

grych commented Aug 19, 2017

@johannesE, could you please check with the fresh drab from master. It should be fixed.

@johannesE
Copy link

Yes, fixed :) Thank you!

@grych grych closed this as completed Aug 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants