Created
December 13, 2017 02:22
-
-
Save BestSonny/c85309df85459998cdeb63c9522fc01e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#I use wsta tool in the command to call websocket | |
wsta 'ws://localhost:3000/socket/websocket' '{"topic":"signup","event":"phx_join","payload":{"login": "ABC", "name": "ABC", "password": "123", "password_confirmation": "123", "email": "[email protected]"},"ref":"1"}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule App.SignupChannel do | |
use App.Web, :channel | |
alias App.User | |
import Ecto.Changeset | |
require Logger | |
def join("signup", _params, socket) do | |
send self(), {:sign_up, _params} | |
{:ok, socket} | |
end | |
def handle_info({:sign_up, _params}, socket), do: socket |> sign_up( _params) | |
defp sign_up(socket, _params) do | |
changeset = User.changeset(%User{}, _params) |> User.with_password_hash | |
changeset = put_change(changeset, :profile_picture, "default_profile.png") | |
case Repo.insert changeset do | |
{:ok, user} -> | |
push socket, "sign_up", %{status: "successfully sign up"} | |
{:error, changeset} -> | |
push socket, "sign_up", %{status: "failed to sign up"} | |
end | |
{:noreply, socket} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule App.UserSocket do | |
use Phoenix.Socket | |
channel "signup", App.SignupChannel | |
transport :websocket, Phoenix.Transports.WebSocket | |
def connect(_params, socket) do | |
{:ok, socket} | |
end | |
def id(_socket), do: nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment