Skip to content
🔥 Crystal client for Phoenix Channels
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs
examples
spec
src
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
shard.yml

README.md

phoenix.cr

Crystal client for Phoenix Channels, based off the official JavaScript reference implementation.

API documentation available here.

Installation

Add this to your application's shard.yml:

dependencies:
  phoenix:
    github: dtcristo/phoenix.cr

Usage

The example below shows basic usage; connecting to a socket, joining a channel, binding to an event and sending messages.

require "phoenix"

# Create socket and connect to it
socket = Phoenix::Socket.new("http://example.com/socket")
socket.connect

# Initiate a channel, bind to an event and join
channel = socket.channel("topic:subtopic")
channel.on "event" do |payload|
  # do stuff with payload
end
channel.join

# Start a loop and send a message down the channel every second
loop do
  sleep 1
  channel.push("new_msg", JSON::Any.new({"text" => JSON::Any.new("Hello world!")}))
end

The Phoenix Channels docs provide details on implementing sockets and channels on the server side. The phoenix.cr docs detail the client side API available for use in your Crystal application.

Chat example

examples/chat.cr demonstates an example chat client.

Start the phoenix-chat server example:

git clone https://github.com/dtcristo/phoenix-chat
cd phoenix-chat
mix deps.get
mix phx.server

Run the chat client:

crystal examples/chat.cr

Follow the prompts to enter your name and chat away.

TODO

  • Add tests
  • Implement Presence
  • Build larger example application

Contributors

  • dtcristo David Cristofaro - creator, maintainer

Credits

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.