Code Monkey home page Code Monkey logo

phoenix_channel_client's Introduction

PhoenixChannelClient

Channel client for connecting to Phoenix

Installation

Add phoenix_channel_client as a dependency in your mix.exs file.

def deps do
  [
    {:phoenix_channel_client, "~> 0.3"},
    {:jason, "~> 1.0"} #optional. You can use your own JSON serializer
  ]
end

Socket

Using the Phoenix Channel Client requires you add a Socket module to your supervision tree to handle the socket connection.

defmodule MySocket do
  use PhoenixChannelClient.Socket, otp_app: :my_app
end

You can configure your socket in the Mix config.

config :my_app, MySocket,
  url: "ws://localhost:4000/socket/websocket",
  serializer: Jason,
  params: %{token: "12345"}

Channels function with callbacks inside a defined module.

defmodule MyChannel do
  use PhoenixChannelClient

  def handle_in("new_msg", payload, state) do
    {:noreply, state}
  end  

  def handle_reply({:ok, "new_msg", resp, _ref}, state) do
    {:noreply, state}
  end
  def handle_reply({:error, "new_msg", resp, _ref}, state) do
    {:noreply, state}
  end
  def handle_reply({:timeout, "new_msg", _ref}, state) do
    {:noreply, state}
  end

  def handle_reply({:timeout, :join, _ref}, state) do
    {:noreply, state}
  end

  def handle_close(reason, state) do
    Process.send_after(self(), :rejoin, 5_000)
    {:noreply, state}
  end
end

Usage

{:ok, socket} = MySocket.start_link(params: %{token: "12345"})
{:ok, channel} = PhoenixChannelClient.channel(MyChannel, socket: MySocket, topic: "rooms:lobby")
MyChannel.join(%{})
MyChannel.leave()

push = MyChannel.push("new:message", %{})
MyChannel.cancel_push(push)

phoenix_channel_client's People

Contributors

mobileoverlord avatar slashmili avatar tlvenn avatar oestrich avatar pinx avatar

Watchers

Rico Moorman avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.