Plan old websockets in phoenix but without magic

how to use pg2 pubsub in raw websocket like phoenix channel please ?

Might be good in a new thread, but what issue were you having with pg2 calls? You should be able to call them straight as normal?

I currently using raw cowboy websocket but dont know how to intergrate pg2 into the app and make a cluster :smiley:

You can use Phoenix.PubSub directly (without Phoenix). By default it uses pg2.

There was the Firenest project, but development has been halted and incorporated into Phoenix.PubSub.

Otherwise there are also libcluster, and swarn, to help You for distribution.

BTW Phoenix channels are using pg2 pubsub mecanism under the hood.

Thanks

I took a look at https://github.com/phoenixframework/phoenix_pubsub

So where do I put this kind of Phoenix.PubSub.subscribe(MyApp.PubSub, "user:123") into ? Maybe my ws handler for each connection ?

I would put it in the init section of your ws handler.

In this init or websocket_init block should I ? :smiley:

 def init(request, _state) do
    state = %{registry_key: request.path}
    opts = %{idle_timeout: 900_000}

    {:cowboy_websocket, request, state, opts}
  end

  def websocket_init(state) do
    {:ok, state}
  end

In the init block.

So I guess the Phoenix.PubSub.subscribe function will subcribe the pid of the websocket with the topic user:123 right ?

Yes, but You also need to define all the handle_info to catch messages from pubsub.

How can I implement the receive handler ? :smiley:

I am not sure, I have never used cowboy_websocket :slight_smile:

But that would look like…

def handle_info(message, state) do
  # do something with the message!

  {:noreply, state}
end

Do you have any example out there please :smiley:

Not related to websocket, but a nice example.

Thanks. :smiley: I will try to grasp it

is it a good design if i seperate a single pubsub module for pg2 and then at each node, this module will use the local registry to send msg to each websocket handler ?

You might be interested in this: PhoenixWS - Websockets over Phoenix Channels