Search⌘ K

Define a New Module

Explore how to define a channel module in Phoenix for Elixir web development. This lesson guides you through setting up the module, routing socket connections, registering the channel, and allowing client joins. Understand the essential configuration needed for real-time, stateful communication between the server and client using Phoenix Channels.

We'll cover the following...

Channel module

The first thing we need is a Channel module. Let’s get one started at islands_interface/lib/islands_interface_web/channels/game_channel.ex:

defmodule IslandsInterfaceWeb.GameChannel do
  use IslandsInterfaceWeb, :channel

  alias IslandsEngine.{Game, GameSupervisor}

To make it behave like a Channel, we use IslandsInterfaceWeb :channel. This triggers the existing channel/0 function in the IslandsInterfaceWeb module, located at islands_interface/lib/islands_interface_web/islands_interface_web.ex.

def channel do
  quote do
    use Phoenix.Channel
    import IslandsInterfaceWeb.Gettext
  end
end

We later work with the Game and GameSupervisor modules, so we need to alias them here. ...