   # Publish message to master's exchange. After master ChatApp receives
   # this message, it will publish 'ADD_USER' message to rest of the cluster.
   defp notify_master_node({conn, :ok, user}) do
-    master_alias = Application.get_env(:user_app, :master_alias)
-
-    options = %{
-      url: Application.get_env(:user_app, :rabbitmq_url),
-      exchange: "#{String.downcase(master_alias)}-exchange",
-      routing_key: "cluster-event"
-    }
-
-    message = %{
-      type: "ADD_USER",
-      payload: UserApp.UserView.render("show.json", user: user)
-    }
-
-    Tackle.publish(Poison.encode!(message), options)
+    UserApp.UserView.render("show.json", user: user)
+    |> publish_message("ADD_USER")
 
     {conn, :ok, user}
   end
   defp notify_master_node(params), do: params
 
+  defp publish_message(payload, type) do
+    options = %{
+      url: Application.get_env(:user_app, :rabbitmq_url),
+      exchange: master_exchange(),
+      routing_key: "cluster-event"
+    }
+
+    message = %{
+      type: type,
+      payload: payload
+    }
+
+    Tackle.publish(Poison.encode!(message), options)
+  end
+
+  defp master_exchange do
+    master_alias = Application.get_env(:user_app, :master_alias)
+
+    "#{String.downcase(master_alias)}-exchange"
+  end
 end

    |> render("error.json", reason: "User does not exist")
  end

  defp delete_host_from_user(conn) do
    user = Guardian.Plug.current_resource(conn)

    User.host_changeset(user, %{host_id: nil})
    |> Repo.update!

    |> Guardian.revoke!

    conn
    |> handle_logout
    |> put_status(:ok)
    |> render("delete.json", success: true)
  end
