nestjs-nats-connector

Crates.ionestjs-nats-connector
lib.rsnestjs-nats-connector
version0.3.0
sourcesrc
created_at2022-02-14 05:42:19.44104
updated_at2022-02-14 11:38:25.332877
descriptionnestjs nats connector
homepage
repositoryhttps://github.com/mohammadjavad948/nestjs-nats-connector
max_upload_size
id532021
size8,166
mohammadjavad (mohammadjavad948)

documentation

README

nestjs-nats-connector

a simple crate that interacts with nestjs over nats

usage

use std::error::Error;
use async_nats::{Connection, Message};
use nestjs_nats_connector::listener;
use nestjs_nats_connector::listener::IncomingRequest;
use serde::{Deserialize, Serialize};
use async_trait::async_trait;

#[derive(Serialize, Deserialize)]
pub struct Pattern {
    cmd: String
}

pub struct Handler {

}

#[async_trait]
impl listener::Listener for Handler {
    type Pattern = Pattern;
    type RequestData = u32;

    async fn handler(
        &self,
        connection: &Connection,
        message: &Message,
        data: IncomingRequest<Self::Pattern, Self::RequestData>
    ) {
        println!("{}", data.id)
    }

    fn get_pattern(&self) -> Self::Pattern {
        Pattern {
            cmd: "dashboard.graph".to_string()
        }
    }
}

impl Handler {
    pub fn new() -> Handler {
        Handler { }
    }
}


async fn test(){
    let connection = async_nats::connect("localhost:4222")
        .await
        .expect("cant connect to nats server");

    nestjs_nats_connector::listener::listen(
        &connection,
        vec![
            Handler::new()
        ]
    ).await;
}
Commit count: 26

cargo fmt