The Phoenix Chat, websockets

I’m having trouble implementing a general lobby channel in my Phoenix app. Overall, Phoenix makes soft real-time communication very simple. A brief reading off Channels provides all the info to get up and running. However, after reaching that point of joining the channel and broadcasting messages it leaves me with some unanswered questions.

Besides the endpoint, are essentially 3 files in play:

  1. socket.js

  2. player_socket.ex

  3. lobby_channel.ex

I wanted to try the scaffolding so I used the generator mix phx.gen.channel Lobby. This seems to have left me with a “lobby:lobby” topic and subtopic. I tried implementing the presence module several times without any success. How do I implement the presence module so that I can show a list of “online users” and also have a username next to the chat message and the time the message was sent in a readable format, opposed to the Date.new() iso format that the guide provides.

I do have a “current_player” object stored in the session so it should be easy to figure this out but for some reason each time I take one step forward I wind up taking two steps back. I’ve reviewed the docs as well as several tutorials but I can’t figure out what I’m doing wrong.

The whole repo is here.

My player socket:

def connect(%{"token" => token}, socket) do

  case Phoenix.Token.verify(socket, "player socket", token, max_age: @max_age) do
    {:ok, player_id} ->
      {:ok, assign(socket, :player, player_id)}
    {:error, reason} ->
      :error
  end
end

Lobby Channel:

  def join("lobby:lobby", _payload, socket) do
      current_player = socket.assigns.current_player
      players = ChannelMonitor.player_joined("lobby:lobby", current_player)["lobby:lobby"]
      send self, {:after_join, players}
      {:ok, socket}
    end

My Socket.js (which is a mess of failed attempts)

/*jshint esversion: 6 */

// To use Phoenix channels, the first step is to import Socket
// and connect at the socket path in "lib/web/endpoint.ex":
import { Socket } from "phoenix";

var token = $('meta[name=channel_token]').attr('content');
var socket = new Socket('/socket', {params: {token: token}});
socket.connect();

var lobby = socket.channel('lobby:lobby');
lobby.on('lobby_update', function(response) {
  console.log(JSON.stringify(response.players));
});
lobby.join().receive('ok', function() {
  console.log('Connected to lobby!');
});

lobby.on('game_invite', function(response) {
  console.log('You were invited to join a game by', response.username);
});

window.invitePlayer = function(username) {
  lobby.push('game_invite', {username: username});
};



// format timestamp
let formatTimestamp = timestamp => {
    let date = new Date(timestamp);
    return date.toLocaleTimeString();
};

let channel = socket.channel("lobby:lobby", {});
let chatInput = document.querySelector("#chat-input");
let messagesContainer = document.querySelector("#messages");

// listen for "enter" key press
chatInput.addEventListener("keypress", event => {
    if (event.keyCode === 13) {
        channel.push("new_msg", { body: chatInput.value });
        chatInput.value = "";
    }
});

// listen for messages and append to the messagesContainer
channel.on("new_msg", payload => {
    let messageItem = document.createElement("li");
    messageItem.innerText = `[${Date()}] ${payload.body}`;
    messagesContainer.appendChild(messageItem);
});

channel
    .join()
    .receive("ok", resp => {
        console.log("Joined successfully", resp);
    })
    .receive("error", resp => {
        console.log("Unable to join", resp);
    });

export default socket;

// WORKING WITH PHOENIX PRESENCE MODULE //
// channel.on("presence_state", state => {
//     presences = Presence.syncState(presences, state);
//     renderOnlinePlayers(presences);
// });
//
// channel.on("presence_diff", diff => {
//     presences = Presence.syncDiff(presences, diff);
//     renderOnlinePlayers(presences);
// });
// WORKING WITH PHOENIX PRESENCE MODULE //

@EssenceOfChaos have you tried using Presence.track/3? The presence docs should be able to get you setup fine on the Elixir side: https://hexdocs.pm/phoenix/Phoenix.Presence.html

@axelson thanks for the reply. I implement the Presence tracking in the lobby channel after joining

1 Like

I’m glad that you got it to work!

@axelson I can get the chat to work, but once i implement Presence then I get the error “unable to connect to websocket”. I can’t get the Presence/Token modules to work, I’m not sure why. A call to IO.inspect token shows that the token is " ".

Please show us how You implement Presence. My config is in the channel file, like this

  ## HANDLE_INFO
  def handle_info(:after_join, socket) do
    {:ok, _} = Presence.track(socket, socket.assigns.current_user.id, %{
      name: socket.assigns.current_user.name,
      online_at: System.system_time(:seconds)
    })
    
    push socket, "presence_state", Presence.list(socket)
    {:noreply, socket}
  end

UPDATE: Sorry, I just found the link to your repo, I will check…

This reminds me this blogpost about poker with elixir.

In the socket You store player_id

{:ok, assign(socket, :player, player_id)}

but in the channel You load current_player

current_player = socket.assigns.current_player

Probably You need something like this in the case statement of your player socket.

{:ok, player_id} ->
      player = Gofish.Accounts.get_player!(player_id)
      {:ok, assign(socket, :current_player, player)}

@kokolegorille I am still unable to connect.
this is my connect function but I can’t seem to connect to the websocket

def connect(%{"token" => token}, socket) do
  IO.puts "#########TOKEN############"
  IO.inspect token
  IO.puts "#########TOKEN############"
  IO.puts "#########SOCKET############"
  IO.inspect socket
  IO.puts "#########SOCKET############"
  case Phoenix.Token.verify(socket, "player auth", token, max_age: @max_age) do
    {:ok, player_id} ->
      player = Gofish.Accounts.get_player!(player_id)
      {:ok, assign(socket, :current_player, player)}
    {:error, _} ->
      :error
  end
end

EDIT: I’m using Token.verify “player auth” in the player_socket and then in the meta tag I am using <%= tag :meta, name: "channel_token", content: Phoenix.Token.sign(@conn, "player auth", @current_player) %>

You should cleanup https://github.com/EssenceOfChaos/gofish/blob/master/assets/js/app.js

// import socket from "./socket";
import { Socket, Presence } from "phoenix";

let socket = new Socket("/socket", {
    params: {}
});

function renderOnlineUsers(presences) {
    let response = "";

    Presence.list(presences, (id, { metas: [first, ...rest] }) => {
        let count = rest.length + 1;
        response += `<br>${id} (count: ${count})</br>`;
    });

    document.querySelector("#UserList").innerHTML = response;
}
socket.connect();

let presences = {};

let channel = socket.channel("lobby:lobby", {});

channel.on("presence_state", state => {
    presences = Presence.syncState(presences, state);
    renderOnlineUsers(presences);
});

channel.on("presence_diff", diff => {
    presences = Presence.syncDiff(presences, diff);
    renderOnlineUsers(presences);
});

channel.join();

to

import socket from "./socket";

Because You declare your socket in socket.js

Yes, but I no longer import socket.js, I’m trying to get the presence module working but without the username coming from the params like in the example. I can’t seem to get presence AND token working together I keep trying different ways

You will need to pass a token as requested in your player_socket

Yes, the token comes from the connect function i posted with the IO.puts and IO.inspects above. I sign the token with the meta tag and verify it in the connect function as per the docs.

Ok, I was refering to your repo code. But if You did the change, I don’t see why it does not hit your connect function.

“no matching clause in PlayerSocket.connect/2” is the error I’m currently getting.

That is so different :slight_smile:

Add something like that to your socket.

def connect(params, _socket), do: IO.inspect params

and try to see the params You get.

I still believe token is not passed right…

I think I may need to add params to the socket declaration in app.js. The Presence guide used

let socket = new Socket("/socket", {
  params: { user_id: window.location.search.split("=")[1] }
})

But since I’m not using a user_id in the params I need it to read the token from the channel_token in the meta tag. So, I think it would be params: {token: "channel_token"}


When I inspect the params in the connect function i get [error] GofishWeb.PlayerSocket.connect/2 returned invalid value %{"vsn" => "2.0.0"}. Expected {:ok, socket} or :error

The error is because my code is incomplete… It should be

def connect(params, socket) do 
  IO.inspect params
  {:ok, socket}
end

BTW It seems You receive %{“vsn” => “2.0.0”} as parameters, but You should receive %{“token” => TOKEN_HERE}

[info] JOIN "lobby:lobby" to GofishWeb.LobbyChannel
  Transport:  Phoenix.Transports.WebSocket (2.0.0)
  Serializer:  Phoenix.Transports.V2.WebSocketSerializer
  Parameters: %{}
[info] Replied lobby:lobby :ok
[error] GenServer #PID<0.445.0> terminating
** (KeyError) key :player_id not found in: %{}
    (gofish) lib/gofish_web/channels/lobby_channel.ex:18: GofishWeb.LobbyChannel.handle_info/2

Given that You extract player_id from the token… It will not work without it. Then the channel will be happy.

@kokolegorille If I put

let socket = new Socket("/socket", { params: { token: "channel_token" } });

Then when I connect and inspect token i will get “channel_token”, which is the name in the meta tag. What else am I doing wrong here?