#ws-rs #pushevent

nightly pushevent

PushEvent is a simple event dispatch library built on top of ws-rs, that allows you to dispatch events to clients based on what resource they are subscribed to

4 releases

0.1.3 Nov 9, 2019
0.1.2 Nov 9, 2019
0.1.1 Nov 8, 2019
0.1.0 Nov 8, 2019

#40 in WebSocket

Download history 2/week @ 2022-03-06 14/week @ 2022-03-13 12/week @ 2022-03-20 14/week @ 2022-04-24 18/week @ 2022-05-01 24/week @ 2022-05-08 68/week @ 2022-05-15 20/week @ 2022-05-22 36/week @ 2022-05-29 22/week @ 2022-06-05 2/week @ 2022-06-12 2/week @ 2022-06-19

66 downloads per month

MIT license

15KB
142 lines

PushEvent

PushEvent is a simple event dispatch library built on top of ws-rs, that allows you to dispatch events to clients based on what resource they are subscribed to.

/// Basic event struct which serializes with serde to json.
#[derive(Serialize, Debug)]
struct SimplePushEvent {
    message: String,
}

impl SerializableEvent for SimplePushEvent {
    /// Serialize method used as a intermediary to serialize the struct into a json string and
    /// return it.
    fn serialize(&self) -> String {
        serde_json::to_string(&self).unwrap()
    }
}

fn main() {
    // Server is started on localhost with port 3012
    let server = Server::new("127.0.0.1:3012");
    let tx = server.get_tx();

    loop {
        // We create a new boxed instance of our SimplePushEvent struct with whatever message
        // inside.
        let msg = Box::new(SimplePushEvent {
            message: String::from("Hello world"),
        });

        // The previous message event is encapsulated in our Event struct to which we supply two
        // arguments, the path/resource subscribers we would like to target ("/hello_world") and
        // our message event struct instance which implements SerializableEvent.
        let event = Event::new("/hello_world", msg);

        // The event is then sent over the tx channel provided by our server instance
        match tx.send(event) {
            Ok(_) => {}
            Err(x) => println!("Err {:?}", x),
        };

        let millis = time::Duration::from_millis(100);
        thread::sleep(millis);
    }
}

Accessing 127.0.0.1:3012/hello_world, for example, will subscribe you to that route, yielding you with a message every 100ms which should be {'message': 'Hello world'}. This can be further used to build and publish more complex events.

Documentation

PushEvent is simple and straightforward to use:

Examples

Rocket ships with an extensive number of examples in the examples/ directory which can be compiled and run with Cargo. For instance, the following sequence of commands builds and runs the Hello, world! example:

cd examples/simple
cargo run

You should see {'message': 'Hello world'} by accessing ws://127.0.0.1:3012.

Testing

To test this library simply run cargo test.

Contributing

Contributions are absolutely, positively welcome and encouraged! Contributions come in many forms. You could:

  1. Submit a feature request or bug report as an issue.
  2. Ask for improved documentation as an issue.
  3. Contribute code via merge requests.

All pull requests are code reviewed and tested by the CI. Note that unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in PushEvent by you shall be licensed under the MIT License without any additional terms or conditions.

License

PushEvent is licensed under the MIT License (LICENSE.md or http://opensource.org/licenses/MIT)

Dependencies

~3.5MB
~100K SLoC