coap

package module
v3.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2025 License: Apache-2.0 Imports: 13 Imported by: 11

README

Go-CoAP

Build Status Quality Gate Status Coverage FOSSA Status sponsors contributors GitHub stars GitHub license GoDoc

The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things. The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.

The go-coap provides servers and clients for DTLS, TCP-TLS, UDP, TCP in golang language.

Features

Requirements

  • Go 1.20 or higher

Samples

Simple
Server UDP/TCP
    // Server

    // Middleware function, which will be called for each request.
    func loggingMiddleware(next mux.Handler) mux.Handler {
        return mux.HandlerFunc(func(w mux.ResponseWriter, r *mux.Message) {
            log.Printf("ClientAddress %v, %v\n", w.Conn().RemoteAddr(), r.String())
            next.ServeCOAP(w, r)
        })
    }

    // See /examples/simple/server/main.go
    func handleA(w mux.ResponseWriter, req *mux.Message) {
        err := w.SetResponse(codes.GET, message.TextPlain, bytes.NewReader([]byte("hello world")))
        if err != nil {
            log.Printf("cannot set response: %v", err)
        }
    }

    func main() {
        r := mux.NewRouter()
        r.Use(loggingMiddleware)
        r.Handle("/a", mux.HandlerFunc(handleA))
        r.Handle("/b", mux.HandlerFunc(handleB))

        log.Fatal(coap.ListenAndServe("udp", ":5688", r))


        // for tcp
        // log.Fatal(coap.ListenAndServe("tcp", ":5688",  r))

        // for tcp-tls
        // log.Fatal(coap.ListenAndServeTLS("tcp", ":5688", &tls.Config{...}, r))

        // for udp-dtls
        // log.Fatal(coap.ListenAndServeDTLS("udp", ":5688", &dtls.Config{...}, r))
    }
Client
    // Client
    // See /examples/simpler/client/main.go
    func main() {
        co, err := udp.Dial("localhost:5688")

        // for tcp
        // co, err := tcp.Dial("localhost:5688")

        // for tcp-tls
        // co, err := tcp.Dial("localhost:5688", tcp.WithTLS(&tls.Config{...}))

        // for dtls
        // co, err := dtls.Dial("localhost:5688", &dtls.Config{...}))

        if err != nil {
            log.Fatalf("Error dialing: %v", err)
        }
        ctx, cancel := context.WithTimeout(context.Background(), time.Second)
        defer cancel()
        resp, err := co.Get(ctx, "/a")
        if err != nil {
            log.Fatalf("Cannot get response: %v", err)
            return
        }
        log.Printf("Response: %+v", resp)
    }
Observe / Notify

Server example.

Client example.

Multicast

Server example.

Client example.

License

Apache 2.0

FOSSA Status

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site.

Backers

Become a backer and get your image on our README on Github with a link to your site.

Documentation

Overview

Package coap provides a CoAP client and server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(network string, addr string, handler mux.Handler) (err error)

ListenAndServe Starts a server on address and network specified Invoke handler for incoming queries.

func ListenAndServeDTLS

func ListenAndServeDTLS(network string, addr string, config *piondtls.Config, handler mux.Handler) (err error)

ListenAndServeDTLS Starts a server on address and network over DTLS specified Invoke handler for incoming queries.

func ListenAndServeDTLSWithOptions added in v3.1.5

func ListenAndServeDTLSWithOptions(network string, addr string, config *piondtls.Config, opts ...dtlsServer.Option) (err error)

ListenAndServeDTLSWithOptions Starts a server on address and network over DTLS specified Invoke options for incoming queries.

func ListenAndServeTCPTLS

func ListenAndServeTCPTLS(network, addr string, config *tls.Config, handler mux.Handler) (err error)

ListenAndServeTCPTLS Starts a server on address and network over TLS specified Invoke handler for incoming queries.

func ListenAndServeTCPTLSWithOptions added in v3.1.5

func ListenAndServeTCPTLSWithOptions(network, addr string, config *tls.Config, opts ...tcpServer.Option) (err error)

ListenAndServeTCPTLSWithOptions Starts a server on address and network over TLS specified Invoke options for incoming queries.

func ListenAndServeWithOptions added in v3.1.5

func ListenAndServeWithOptions(network, addr string, opts ...any) (err error)

ListenAndServeWithOption Starts a server on address and network specified Invoke options for incoming queries. The options is only support tcpServer.Option and udpServer.Option

Types

This section is empty.

Directories

Path Synopsis
examples
dtls/cid/client command
dtls/cid/server command
dtls/pki/client command
dtls/pki/server command
dtls/psk/client command
dtls/psk/server command
mcast/client command
mcast/server command
observe/client command
observe/server command
options/server command
simple/client command
simple/server command
net
pkg
fn
tcp
test
net
Helper package for tests, must not be used in production code.
Helper package for tests, must not be used in production code.
udp

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL