phoenix

Crates.iophoenix
lib.rsphoenix
version0.1.2
sourcesrc
created_at2018-03-17 11:18:22.204161
updated_at2018-03-17 11:50:58.491046
descriptionRust client for Phoenix channels
homepage
repositoryhttps://github.com/laibulle/phoenix-rs
max_upload_size
id56097
size13,145
Marc-Antoine ARNAUD (MarcAntoine-Arnaud)

documentation

README

Phoenix Channels client for Rust

This library is an attempt to create Phoenix client in Rust.

This crate is tested with https://github.com/laibulle/phoenix_channel_demo

example

git clone git@github.com:laibulle/phoenix_channel_demo.git
cd phoenix_channel_demo
mix deps.get
mix phx.server
extern crate phoenix;
#[macro_use]
extern crate serde_json;

extern crate env_logger;

use std::{thread, time};
use phoenix::{Phoenix, Event};

fn main() {
    env_logger::init();
    
    let url = "ws://localhost:4000/socket";

    // Simulate a user
    thread::spawn(move || {
        let mut phx = Phoenix::new(url);
        let mutex_chan = phx.channel("room:lobby").clone();

        {
            let mut device_chan = mutex_chan.lock().unwrap();
            device_chan.join();
        }

        loop {
            match phx.out.recv() {
                Ok(msg) => println!("user1: {:?}", msg),
                Err(_err) => ()//println!("{:?}", err)
            }
        }
    });

    thread::sleep(time::Duration::from_millis(500));

    // Simulate an other user
    let mut phx = Phoenix::new(url);
    let mutex_chan = phx.channel("room:lobby").clone();

    {
        let mut device_chan = mutex_chan.lock().unwrap();
        device_chan.join();
        device_chan.send(Event::Custom("new_msg".to_string()), serde_json::from_str(r#"{"body": "Hello"}"#).unwrap());
    }

    loop {
        match phx.out.recv() {
            Ok(msg) => println!("user2: {:?}", msg),
            Err(_err) => ()//println!("{:?}", err)
        }
    }
}

RUST_LOG=debug cargo run
Commit count: 0

cargo fmt