plgd-dev / go-coap

Implementation of CoAP Server & Client in Go
https://coap.technology
Apache License 2.0
450 stars 116 forks source link

Responses to non-confirmable requests (RFC7252 section 5.2.3) #585

Open theisHafele opened 1 month ago

theisHafele commented 1 month ago

From RFC7252 section 5.2.3

If the request message is Non-confirmable, then the response SHOULD be returned in a Non-confirmable message as well. However, an endpoint MUST be prepared to receive a Non-confirmable response (preceded or followed by an Empty Acknowledgement message) in reply to a Confirmable request, or a Confirmable response in reply to a Non-confirmable request.

Is this currently supported/handled by the library? If not, what's the idiomatic way of preparing the endpoint as a client using the go-coap library?

jkralik commented 1 month ago

Yes, it is supported. By default, it sends a confirmable response, but you can set the message type to Non/Con by using the function func (r *Message) SetType(typ message.Type). This type is also used throughout the entire blockwise transfer process.

theisHafele commented 1 month ago

Thanks for the response. Sorry, I should have been more clear. I meant the part that says:

Confirmable response in reply to a Non-Confirmable request

So as a client sending a non-confirmable request, is a confirmable response handled or would I need to set up a server to handle it as a request?

jkralik commented 3 weeks ago

It's up to you to set the desired message type in the server handler. By default, if a non-confirmable request is received, the go-coap server sends a response with a confirmable message type, requiring the client to send an acknowledgment. However, you can use the SetType function to override this and have the server reply with a non-confirmable message as well.