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

Allow client-side values to be sent to a LiveView #204

Closed
alexgaribay opened this issue May 3, 2019 · 4 comments
Closed

Allow client-side values to be sent to a LiveView #204

alexgaribay opened this issue May 3, 2019 · 4 comments

Comments

@alexgaribay
Copy link
Member

As discussed with @chrismccord in Slack, we need a way to pass client-side values to a LiveView. An example use-case is being able to send in timezone information coming from the browser on socket connect.

@hubertlepicki
Copy link

Is this the same as sending custom events from users' javascript?

I am using something like this, although pretty sure I'm abusing private API and it's not entirely nice:

const target = document.querySelector("[data-phx-view]")
const phxEvent = {"my": "event", "payload": "here"}
liveSocket.owner(target, view => view.pushEvent("customevent", target, phxEvent))

which then triggers appropriate clause in handle_info in LiveView.

Are we talking here about wrapping this up in a nicer public interface?

@alexgaribay
Copy link
Member Author

That would be one aspect of it. Another aspect is sending connection params to the socket like we already can with a normal channels socket but for a LiveSocket. Some values are only available from the browser like locale and timezone information so we need some way of passing it down to a LiveView.

let socket = new LiveSocket("/live", {params: {timezone: Intl.DateTimeFormat().resolvedOptions().timeZone}})

@ashton314
Copy link

I'm running into this need too. Maybe another thing to consider: do we open up things like browser local storage to LiveView?

@chrismccord
Copy link
Member

Closed via #239

You can now do:

let liveSocket = new LiveSocket("/live", {logger: logger, params: {foo: "bar"}})

and then they will be available during connected mount via get_connect_params/1. See LiveView.get_connect_params/1 docs for more details. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants