Elixir Phoenix PubSub Tutorial: Difference between revisions

From ElixirBlocks
Jump to: navigation, search
No edit summary
No edit summary
 
(38 intermediate revisions by the same user not shown)
Line 2: Line 2:
__TOC__
__TOC__
<hr>
<hr>
'''Prerequisites'''
You should know how to launch a 1 page Live View app that lets the user submit data via a form. The form data should print to the page.
<hr/>
This tutorial explains the basics of building a Phoenix PubSub chat app.  
This tutorial explains the basics of building a Phoenix PubSub chat app.  


The goal of this writing is to explain how to write code used to build PubSub applications in Phoenix.  I tried to write examples that keep code writing to a minimum. I believe minimalist examples are the best approach toward comprehension.
===What is PubSub?===
PubSub is the Phoenix frameworks real time publish/subscribe service. This is an API that lets developers write applications that update in near real time such as chat rooms and multi-player games.
 
 
The goal of this writing is to explain how to use the code provided by the PubSub API to build real time applications.  
 


===Project Description===
The tutorial is divided into three parts, each part building on the completion of the previous one.
The tutorial is divided into three parts, each part building on the completion of the previous one.


'''Project Description'''
* '''Part 1''': We create a two route live view app with each route assigned its own page. The two routes are '''/send''' and '''/receive'''.
 
* '''Part 1''': We create a two route live view app with each route assigned its own page. There are two routes are '''/send''' and '''/receive'''.
The page at '''/send''' contains an html form and form submission code. When data is submitted, the '''/receive''' page receives the data, and renders it to its page. The code we write for this exercise uses pubsub functions to connect the two LiveView pages.  
The page at '''/send''' contains an html form and form submission code. When data is submitted, the '''/receive''' page receives the data, and renders it to its page. The code we write for this exercise uses pubsub functions to connect the two LiveView pages.  


Line 67: Line 82:
Go to the router and update it as shown.
Go to the router and update it as shown.


app/app_web/router.ex
'''directory:'''  app/app_web/router.ex


<source>
<source>
Line 80: Line 95:
== Create the LiveView Pages ==
== Create the LiveView Pages ==


Create a new folder named live in this directory: '''App/lib/app_web'''. The end result will look like this: '''App/lib/app_web/live'''.
Create a new folder named live in this directory: '''App/lib/app_web'''. The end result looks like this: '''App/lib/app_web/live'''.


In the '''live''' directory, create two files and name them '''send_live.ex''' and '''receive_live.ex'''.
In the '''live''' directory, create two files and name them '''send_live.ex''' and '''receive_live.ex'''.
Line 98: Line 113:
   end
   end


  def handle_event("send", %{"text" => text}, socket) do
    IO.inspect(text)


  def render(assigns) do
    Phoenix.PubSub.broadcast(App.PubSub, "message", {:pubsub_transmission, text})
    ~H"""
    <div>
      <h1>Send Connection</h1>


   
     {:noreply, socket}
     </div>
    """
   end
   end
end
</source>
==Edit Receive_Live.ex ==
Open the file named receive_live.ex and paste the following code into it:
<source>
defmodule AppWeb.ReceiveLive do
  use AppWeb, :live_view


  def mount(_params, _session, socket) do
    {:ok, socket}
  end


   def render(assigns) do
   def render(assigns) do
     ~H"""
     ~H"""
     <div>
     <div>
       <h1>Receive Connection</h1>
       <h1>Send Message</h1>
 
      <form phx-submit="send">
 
        <input type="text" name="text" />
        <button type="submit">Send</button>
      </form>
     </div>
     </div>
     """
     """
   end
   end
end
</source>
== Validate Your Work ==
Ensure the code you wrote has no errors by running the server and checking each route.
mix phx.server
Go to: 
localhost:4000/send
localhost:4000/receive
The pages should load without error.
== Creating a Form and its Event Handler ==
The first step to send data via a form is to create the form and write an event handler to listen and receive data from the form. You should have enough prerequisite knowledge to know how to do this. If you don't read this tutorial before you proceed:
[[Forms and Event Handlers in Elixir Phoenix|Forms and Event Handlers in Elixir Phoenix]]
Update the send_live.ex to reflect the code below:
<source>
defmodule AppWeb.SendLive do
  use AppWeb, :live_view
 
  def mount(_params, _session, socket) do
    {:ok, socket}
  end
 
 
  def handle_event("send", %{"text" => text}, socket) do
    IO.inspect text
    {:noreply, socket}
  end
 
  def render(assigns)do 
  ~H"""
<div>
  <h1>Send Message</h1>
  <form phx-submit="send">
    <input type="text" name="text" />
    <button type="submit">Send</button>
  </form>
</div>
"""
  end
 
end
end


</source>
</source>


Ensure it works by submitting form data. In the terminal console you will see the output.
== Before We Add the PubSub Code ==
You are now going to perform these actions.


1. Add code designed to broadcast a change
Open receive_live.ex and copy the following code to it.
 
2. Add code designed to subscribe to that change
 
Both of these code pieces act similar to event handlers/listeners in that they listen to events that the developer designates and performs an action in response.
 
Both broadcast code and subscribe code can be placed in different parts of your codebase depending on your goal.
 
For this exercise you write the broadcast code to your '''SendLive''' module and write the subscribe code to your '''ReceiveLive''' module.
 
===Add the PubSub Broadcast Code to SendLive Module===


<source>
<source>
 
defmodule AppWeb.ReceiveLive do
 
defmodule AppWeb.SendLive do
   use AppWeb, :live_view
   use AppWeb, :live_view
 
  def mount(_params, _session, socket) do
    {:ok, socket}
  end
 
 
  def handle_event("send", %{"text" => text}, socket) do
    IO.inspect text
    AppWeb.Endpoint.broadcast(topic, "message", text) # Broadcast
    {:noreply, socket}
  end
 
  defp topic do #Topic
    "chat"
  end
  def render(assigns)do 
  ~H"""
<div>
  <h1>Send Message</h1>
  <form phx-submit="send">
    <input type="text" name="text" />
    <button type="submit">Send</button>
  </form>
</div>
"""
  end
 
end
</source>
===Add the PubSub Subscribe Code to ReceiveLive Module===
<source>


defmodule AppWeb.ReceiveLive do
  use AppWeb, :live_view
 
   def mount(_params, _session, socket) do
   def mount(_params, _session, socket) do
     if connected?(socket) do
     if connected?(socket) do
       AppWeb.Endpoint.subscribe(topic)     # PupSub Subscribe
       Phoenix.PubSub.subscribe(App.PubSub, "message")
     end
     end


     {:ok, assign(socket, messages: "")}
     {:ok, assign(socket, message_item: "")}
  end
 
  def handle_info(%{event: "message", payload: message}, socket) do  # Handle Ifno is needed
    IO.inspect message
    {:noreply, assign(socket, messages: message)}
   end
   end
 


   defp topic do # Topic
   def handle_info({:pubsub_transmission, text}, socket) do
     "chat"
     {:noreply, assign(socket, message_item: text)}
   end
   end


   def render(assigns)do  
   def render(assigns) do
  ~H"""
    ~H"""
    <div>
    <div>
    <h1>ChatLive</h1>
      <h1>ChatLive</h1>
    <%= @messages %>
      <%= @message_item %>
 
 
     </div>
     </div>
 
    """
    """
   end
   end
 
end
end
</source>
== PubSub Code Result and Explanation==
Open the app in two browser tabs and set one to /send and one to /receive.
Submit a form in /send and you will see the result in /receive


=== SendLive PubSub Code Explained===
The SendLive module contains this code:


<source>
AppWeb.Endpoint.broadcast(topic, "message", text)
</source>
</source>


In all default Phoenix applications the PubSub code is in the module named Endpoint. The above example is of a PubSub function named broadcast. When working with <pre> AppWeb.Endpoint </pre> the function you use with either be <pre>subscribe</pre> or <pre>broadcast</pre>.


<source>
NameOfApp.Endpoint.a_broadcast_or_subscribe_function_goes_here
</source>


The broadcast function has three arguments. In our examples they are named topic, "message" and text.


<source>
Launch the server and open up two browser tabs. Set one to '''/send''' and the other to '''/receive'''
AppWeb.Endpoint.broadcast(topic, "message", text)
</source>


The first argument is the name of the topic. The topic is what connects the broadcast and the subscriber. You can have many broadcasters and subscribers all with different topics.
Submit data from '''/send''' and view the result on '''/receive'''.


A topic is a string that acts as an ID to connect the broadcast and subscriber. In our example the topic is "chat" and is referenced in a private function named topic.
Play with the code and explore it. Ensure that it works.


<source>
===Explanation===


  defp topic do #Topic
Before explaining the code, the following summarizes the important parts applicable to all PubSub applications. Direct your attention to the three functions below. These functions are the main tools that you use when working with the PubSub API and it is important to get familiar with them.
    "chat"
  end


</source>
* Phoenix.PubSub.'''broadcast'''(App.PubSub, "message", {:pubsub_transmission, text})
* Phoenix.PubSub.'''subscribe'''(App.PubSub, "message") 
* ''' handle_info'''({:pubsub_transmission, text}, socket)


The code will work if you place the string in the broadcast function directly, like this:
For brevity I refer to these as '''broadcast''','''subscribe''' and '''handle_info'''.


AppWeb.Endpoint.broadcast('''"chat"''', "message", text)
All PubSub applications are composed of these three functions.


The second argument creates another connection similar to the first argument. The second argument connects your broadcast function to an additional <pre>handle_info</pre> function. This function
===A Simple Mental Model===
allows you to change state and affect your app in some way.


Mental Models are helpful not because they are 100 percent accurate but because they are tangibly useful. The mental model I use is as follows:


<blockquote>
The '''broadcast''' and '''subscribe''' functions are used to send data from one module to another. You can look at these functions as two ends of a physical cable that connects modules together.
<hr/>
The module(s) that transmits the data must contain the '''broadcast''' function in it's code, the module(s) receiving the data must contain the '''subscribe''' function in it's code.
The '''handle_info''' function is not specific to PubSub. It is a function used with Elixir GenServers. For this article I suggest you ignore this reality and simply look at '''handle_info''' as a tool to configure a PubSub application. If you want a more in depth explanation it will not be given in this article.
<hr/>
</blockquote>


The receiving module must also contain a handle_info function. This function acts as an event handler that listens for and captures the data.




Keep a mental place holder to remember the following statement: 


====Phoenix.PubSub.broadcast====


'''The PubSub broadcast function needs to connect to two other functions.'''
1. In SendLive the code Phoenix.PubSub.broadcast is used to broadcast message data. This method takes three arguments.


* '''Subscribe'''
* The pubsub type
* '''Handle_info'''
* The topic
* The payload (the data you want to send to all listeners)


The pubsub type is always going to be named your-app dot PubSub (such as App.PubSub) For our purposes you can treat this argument as boiler plate code. 


Phoenix.PubSub.broadcast('''App.PubSub''')


The third argument is the data you want to transmit via the web socket connection. In the example code below the data is a variable named text. This variable could be user submitted form data, data from a database or any other data. It can be in the form of a list , string, map etc.


<source>
The second argument, called the topic, is a string that represents a connection between the broadcast function and a subscribe function. The topic connects them together.
AppWeb.Endpoint.broadcast("chat", "message", text)
</source>
 
 
=== ReceiveLive PubSub Code Explained===
 
To respond to the broadcast message the app needs 2 things to listen for its transmission.
 
* subscribe function
* handle_info function  
 
Say it again, when you broadcast a PubSub message you need two things to listen for the broadcast transmission.
 
* subscribe function  
* handle_info function
 
==== Subscribe ====
 
The subscribe function gives the module the ability to listen to the broadcast function with the same topic. Without it, the handle_info function will not respond to broadcast changes.  


You can look at the code below as boiler plate needed to enable the handle_info function to listen for the broadcast message.
Phoenix.PubSub.broadcast( App.PubSub, '''"some-topic-goes-here"''' )


<source>
The third argument is the payload. This can be any data you want to transmit between Live Views.
if connected?(socket) do
      AppWeb.Endpoint.subscribe(topic)    # PupSub Subscribe
    end
</source>


The topic is referenced from a private function but it doesn't have to be. You can hardcode the topic and it will work.


<source>
Phoenix.PubSub.broadcast( App.PubSub, "some-topic-goes-here",'''%{data: "hello world}''')
    if connected?(socket) do
      AppWeb.Endpoint.subscribe("chat")     # PupSub Subscribe
    end
</source>


====Phoenix.PubSub.subscribe====
The first two arguments of subscribe mirror broadcast.


==== Handle Info Function====
* Phoenix.PubSub. subscribe ('''App.PubSub, topic''')


<source>
To use subscribe in a Module, you place it in the '''mount''' function.
  def handle_info(%{event: "message", payload: message}, socket) do  # Handle Info is needed
    IO.inspect message
    {:noreply, assign(socket, messages: message)}
  end
</source>


As previously mentioned, this function responds to the invoking of this function:
====handle_info====


<source>
This is a function that is invoked on any "send" event to a process. In our code it is used as a tool to capture the data being subscribed to.
AppWeb.Endpoint.broadcast("chat", "message", text)
</source>
 
These two functions are connected via the string "message".
The handle_info function has two arguments The first argument is a map that contains two keys.
 
<source>
%{event: "message", payload: message}
</source>
 
The first key is named event. This is assigned the same string as the second argument of:
 
<source>
AppWeb.Endpoint.broadcast("chat", "message", text)
</source>
 
 
The second key of the map is the payload data.
 
<source>
%{event: "message", payload: message}
</source>
 
The payload is the data that has been received via the third argument of the broadcast function. In this case, it is named text.
 
<source>
AppWeb.Endpoint.broadcast("chat", "message", text)
</source>

Latest revision as of 07:29, 29 October 2023

This page is in progress


Prerequisites

You should know how to launch a 1 page Live View app that lets the user submit data via a form. The form data should print to the page.




This tutorial explains the basics of building a Phoenix PubSub chat app.

What is PubSub?

PubSub is the Phoenix frameworks real time publish/subscribe service. This is an API that lets developers write applications that update in near real time such as chat rooms and multi-player games.


The goal of this writing is to explain how to use the code provided by the PubSub API to build real time applications.


Project Description

The tutorial is divided into three parts, each part building on the completion of the previous one.

  • Part 1: We create a two route live view app with each route assigned its own page. The two routes are /send and /receive.

The page at /send contains an html form and form submission code. When data is submitted, the /receive page receives the data, and renders it to its page. The code we write for this exercise uses pubsub functions to connect the two LiveView pages.

When complete, you will have two browser tabs open, one opened to /send and one opened to /receive. The /send route sends data to /receive and you view the update in real time.

  • Part 2: We convert the previous app into a single LiveView page that performs both actions on a single route. The end result is the creation of a single page chat application containing real time updates that you view across browser tabs.
  • Part 3: We write code to update your single page app to store chat data in a database.



Part 1

Setup

To begin we will start with an new Phoenix instance. In your terminal create a new phoenix app by typing:

mix phx.new app

When the console prompts you to Fetch and install dependencies, choose yes.

When you see the following instructions configure your database, run mix ecto.create.



We are almost there! The following steps are missing:

    $ cd app

Then configure your database in config/dev.exs and run:

    $ mix ecto.create

Start your Phoenix app with:

    $ mix phx.server

You can also run your app inside IEx (Interactive Elixir) as:

    $ iex -S mix phx.server



If you are new to Elixir and are having trouble with database setup, please fix those issues before proceeding with this tutorial.

When the app is created, open your web browser to localhost:4000. The app will launch. If it doesn't, fix the errors before moving forward.


Create Routes

Go to the router and update it as shown.

directory: app/app_web/router.ex

  scope "/", AppWeb do
    pipe_through :browser
    live "/send", SendLive, :home
    live "/receive", ReceiveLive, :home
    get "/", PageController, :home
  end

Create the LiveView Pages

Create a new folder named live in this directory: App/lib/app_web. The end result looks like this: App/lib/app_web/live.

In the live directory, create two files and name them send_live.ex and receive_live.ex.


Edit Send Live.ex

Open the file send_live.ex in your text editor. Paste the following code into it.

defmodule AppWeb.SendLive do
  use AppWeb, :live_view

  def mount(_params, _session, socket) do
    {:ok, socket}
  end

  def handle_event("send", %{"text" => text}, socket) do
    IO.inspect(text)

    Phoenix.PubSub.broadcast(App.PubSub, "message", {:pubsub_transmission, text})

    {:noreply, socket}
  end


  def render(assigns) do
    ~H"""
    <div>
      <h1>Send Message</h1>
      <form phx-submit="send">
        <input type="text" name="text" />
        <button type="submit">Send</button>
      </form>
    </div>
    """
  end
end


Open receive_live.ex and copy the following code to it.

defmodule AppWeb.ReceiveLive do
  use AppWeb, :live_view

  def mount(_params, _session, socket) do
    if connected?(socket) do
      Phoenix.PubSub.subscribe(App.PubSub, "message")
    end

    {:ok, assign(socket, message_item: "")}
  end

  def handle_info({:pubsub_transmission, text}, socket) do
    {:noreply, assign(socket, message_item: text)}
  end

  def render(assigns) do
    ~H"""
    <div>
      <h1>ChatLive</h1>
      <%= @message_item %>
    </div>
    """
  end
end



Launch the server and open up two browser tabs. Set one to /send and the other to /receive

Submit data from /send and view the result on /receive.

Play with the code and explore it. Ensure that it works.

Explanation

Before explaining the code, the following summarizes the important parts applicable to all PubSub applications. Direct your attention to the three functions below. These functions are the main tools that you use when working with the PubSub API and it is important to get familiar with them.

  • Phoenix.PubSub.broadcast(App.PubSub, "message", {:pubsub_transmission, text})
  • Phoenix.PubSub.subscribe(App.PubSub, "message")
  • handle_info({:pubsub_transmission, text}, socket)

For brevity I refer to these as broadcast,subscribe and handle_info.

All PubSub applications are composed of these three functions.

A Simple Mental Model

Mental Models are helpful not because they are 100 percent accurate but because they are tangibly useful. The mental model I use is as follows:

The broadcast and subscribe functions are used to send data from one module to another. You can look at these functions as two ends of a physical cable that connects modules together. The module(s) that transmits the data must contain the broadcast function in it's code, the module(s) receiving the data must contain the subscribe function in it's code.

The receiving module must also contain a handle_info function. This function acts as an event handler that listens for and captures the data.


Phoenix.PubSub.broadcast

1. In SendLive the code Phoenix.PubSub.broadcast is used to broadcast message data. This method takes three arguments.

  • The pubsub type
  • The topic
  • The payload (the data you want to send to all listeners)

The pubsub type is always going to be named your-app dot PubSub (such as App.PubSub) For our purposes you can treat this argument as boiler plate code.

Phoenix.PubSub.broadcast(App.PubSub)


The second argument, called the topic, is a string that represents a connection between the broadcast function and a subscribe function. The topic connects them together.

Phoenix.PubSub.broadcast( App.PubSub, "some-topic-goes-here" )

The third argument is the payload. This can be any data you want to transmit between Live Views.


Phoenix.PubSub.broadcast( App.PubSub, "some-topic-goes-here",%{data: "hello world})

Phoenix.PubSub.subscribe

The first two arguments of subscribe mirror broadcast.

  • Phoenix.PubSub. subscribe (App.PubSub, topic)

To use subscribe in a Module, you place it in the mount function.

handle_info

This is a function that is invoked on any "send" event to a process. In our code it is used as a tool to capture the data being subscribed to.