vlad.grb

vlad.grb

Broadcast to socket without channel

I checked on the forum but still didn’t find a response
Is there any way to send a push to socket without channels. LIke my UserSocket is connected and I want to make something Endpoint.brocast("user_socket:foo", "bar", %{"foo" => "bar"})

First Post!

aseigo

aseigo

It may be easier to find an answer if you share what effect you are trying to achieve.

Are you trying to send a message to exactly one of the user’s connections? i.e. they have multiple browsers / mobile app connections and you want to send a message to a specific one, but not all the others? Or something else?

Most Liked

aseigo

aseigo

Looking into it more, you do apparently need to connect to a channel. :frowning: sorry for the noise.

However, you can easily create a users: channel that does not require any special auth:

  def join(<<"user:", id :: binary>>, payload, socket) do
    if socket.assigns.user.id == id do
      send(self(), :after_join)
      {:ok, socket}
    else
      {:error, %{reason: "unauthorized"}}
    end
  end

.. or similar.

and then you can broadcast to the channel. Is there a specific reason you are avoiding connecting to the channel?

aseigo

aseigo

Channels do not create an extra network connection. They are multiplexed over the socket. On the server-side channels are quite cheap. So I wouldn’t worry about that.

idi527

idi527

You might try running <YourApp>.Endpoint.subscribe("<some_topic>") from within the connect/2 callback. Supposedly, it would run inside the process handling the socket. Then you would be able to broadcast to all socket processes subscribed to "<some_topic>" with <YourApp>.Endpoint.broadcast("<some_topic>", some_event, some_msg) and see where it gets you. If phoenix’s socket is just a handler module for cowboy, you might be able to use cowboy’s callbacks to handle incoming messages, which IIRC is websocket_info, which seems to be forwarded to handler’s ws_info in phoenix v1.3 and to handle_info in phoenix master.

if there is no good API then one really should be PR’d in to handle arbitrary unhandled messages to the socket PID

It seems like handle_info has been added as a possible callback to phoenix.socket in v1.4. But in v1.3 the messages are simply ignored unless you pack them in a {:socket_push, _, _encoded_payload} tuple.

Last Post!

idi527

idi527

You need to use proper values instead of "asdf" for what you actually want to send to the client.

For example

send(socket_pid, {:socket_push, :text, Poison.encode!(%{"oh" => "my"})})

Where Next?

Popular in Questions Top

New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement