#websocket #phoenix #channels #pubsub #messaging

phoenix-channels

A websocket client library for Phoenix channels

5 releases

Uses old Rust 2015

0.1.4 Oct 12, 2019
0.1.3 Apr 17, 2018
0.1.2 Apr 17, 2018
0.1.1 Mar 25, 2018
0.1.0 Mar 17, 2018

#48 in WebSocket

Download history 2/week @ 2022-03-07 16/week @ 2022-03-14 17/week @ 2022-03-21 1/week @ 2022-04-04 2/week @ 2022-04-18 30/week @ 2022-04-25 42/week @ 2022-05-02 5/week @ 2022-05-09 89/week @ 2022-05-16 29/week @ 2022-05-23 40/week @ 2022-05-30 21/week @ 2022-06-06 1/week @ 2022-06-13 5/week @ 2022-06-20

77 downloads per month

MIT license

16KB
342 lines

phoenix-channels-rs

A Rust library providing functionality to connect to a phoenix channel.

It is intended to become at least a production grade client however it is very early in development and as such should be treated as experimental.

Currently there are two ways to use this library. The first is a high level threaded client and the second is a low level sender/receiver.

Connecting via the client

Connecting via the client will return a tuple with the Client struct and a mpsc::Receiver of processed incoming messages. This allows for messages to be received in a separate thread if desired.

An very basic example of connecting with the Client:

use std::thread;

extern crate phoenix_channels;
use phoenix_channels::client;


let url = "ws://localhost:4000/socket";

let token = "abcde12345";
let params = vec![("token", token)];

let (client, messages) = client::Client::new(url, params, None).unwrap();

thread::spawn(move || {
    for message in messages {
        println!("{:?}", message);
    }
});

client.join("room:lobby").unwrap();

The client itself will handle the heartbeat and anything else required to deliver a serde json struct to the message iterator result. However, it is an opinionated implementation of a phoenix client and uses threading, mutexes and channels to achieve its end goal of an easy to use interface. This may not be desirable for scenarios where more control is required.

Connecting via the Sender/Receiver

An alternative to the high level Client is to purely use the low level Sender and Receiver structs and handle the threading and heartbeats yourself. The processing of messages is still handled and methods such as join and heartbeat are still conveniently available on the Sender struct (the Client library actually uses these under the hood).

Here is an example of how one can use the Sender/Receiver structs themselves:

use std::thread;
use std::time::Duration;

extern crate phoenix_channels;
use phoenix_channels::client;

let url = "ws://localhost:4000/socket";

let token = "abcde12345";
let params = vec![("token", token)];

let (mut sender, receiver) = client::connect(url, params, None).unwrap();

sender.join("room:lobby").unwrap();

// create a heartbeat thread
thread::spawn(move || {
    loop {
        sender.heartbeat().unwrap();
        thread::sleep(Duration::from_secs(2));
    }
});

for message in receiver {
    println!("{:?}", message);
}

Using the lower level API requires more work but it gives you full control over the threading and flow of messages being sent and received to the server.

Logging

Both the high level and low level APIs use slog under the hood for structured logging. To enable this you can pass your logger via both connect and Client::new by specifying Some(logger) where the examples currently specify None.

Dependencies

~6.5MB
~163K SLoC