Phoenix channel get user id

I’m currently experimenting with Phoenix channels and I was wondering if there is simple way to know what user send a message. I haven’t found a way of getting the userid into the javascript so i was wondering if there was a simple way using the cannel itself. I don’t need authentication, thats why I found this Tutorial not so helpful.
I just need to know who send something.

Hi! I think that it should be possible to simply pass a user id to the page if you are using phoenix templates (<%= @user_id %>, take a look at the view guide), or create an endpoint that returns it if you are creating a JSON API (/api/get_user → {"id": 123}). Then you can pass this to the channel constructor client-side like you would the token in this tutorial.

This would definitely allow you to pass the user id without doing authentication, in other words, in a rather horrifically insecure way. What you could also do is wrap the user id in a call to Phoenix.Token.sign(Endpoint, "my socket tokens", user_id) and in the channel decode it using {:ok, user_id} = Phoenix.Token.verify(Endpoint, "my socket tokens", token), which is almost no extra work and has the added benefit of teaching you how amazingly simple it is to use Phoenix tokens :wink: Take a look at Channels — Phoenix v1.7.10 under the header “Using token authentication”.