Skip to content

jasta/coap-server-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Robust, ergonomic Rust CoAP server

An asynchronous CoAP server with a modern and ergonomic API for larger scale applications, inspired by warp and actix. CoAP offers an excellent alternative to HTTP for resource constrained environments like IoT devices.

  • Ergonomic: Fluent app-builder API makes it easy to compose rich applications, including those that use more advanced CoAP features.
  • Concurrent: High concurrency is supported by using a separate spawned task for each request, allowing long running requests to not interfere with shorter running ones at scale.
  • Feature-rich: Supports a wide range of CoAP server features including Observe, and Block-wise Transfer.
  • Flexible: Supports pluggable transport backends with goals of supporting alternative async runtimes like embassy.

Status

GitHub tag Build Status MIT licensed

This project is a work in progress, attempting to provide a rich and stable codebase for further development. See Project Status tracking issue for more info.

Example

use coap_server::app::{CoapError, Request, Response};
use coap_server::{app, CoapServer, FatalServerError, UdpTransport};

#[tokio::main]
async fn main() -> Result<(), FatalServerError> {
    let server = CoapServer::bind(UdpTransport::new("0.0.0.0:5683")).await?;
    server.serve(
        app::new().resource(
            app::resource("/hello").get(handle_get_hello))
    ).await
}

async fn handle_get_hello(request: Request<SocketAddr>) -> Result<Response, CoapError> {
    let whom = request
        .unmatched_path
        .first()
        .cloned()
        .unwrap_or_else(|| "world".to_string());

    let mut response = request.new_response();
    response.message.payload = format!("Hello, {whom}").into_bytes();
    Ok(response)
}

To experiment, I recommend using the excellent coap-client command-line tool, as with:

$ coap-client -m get coap://localhost/hello
Hello, world

Related Projects