{"action":"created","issue":{"url":"https://api.github.com/repos/mcollina/node-coap/issues/39","labels_url":"https://api.github.com/repos/mcollina/node-coap/issues/39/labels{/name}","comments_url":"https://api.github.com/repos/mcollina/node-coap/issues/39/comments","events_url":"https://api.github.com/repos/mcollina/node-coap/issues/39/events","html_url":"https://github.com/mcollina/node-coap/issues/39","id":57575741,"number":39,"title":"Listening on the same port used for sending","user":{"login":"dmoranj","id":1152892,"avatar_url":"https://avatars.githubusercontent.com/u/1152892?v=3","gravatar_id":"","url":"https://api.github.com/users/dmoranj","html_url":"https://github.com/dmoranj","followers_url":"https://api.github.com/users/dmoranj/followers","following_url":"https://api.github.com/users/dmoranj/following{/other_user}","gists_url":"https://api.github.com/users/dmoranj/gists{/gist_id}","starred_url":"https://api.github.com/users/dmoranj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmoranj/subscriptions","organizations_url":"https://api.github.com/users/dmoranj/orgs","repos_url":"https://api.github.com/users/dmoranj/repos","events_url":"https://api.github.com/users/dmoranj/events{/privacy}","received_events_url":"https://api.github.com/users/dmoranj/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-13T09:16:00Z","updated_at":"2015-02-13T16:31:59Z","closed_at":null,"body":"I'm currently involved in the development of a OMA Lightweight M2M library, based on node-coap (you can find it [here](https://github.com/telefonicaid/lwm2m-node-lib) and I'm struggling with one aspect of the specification: it requires the server to communicate with the devices **using the same IP and Port** used in the registration message (a POST COAP message to the server). That IP and port is used then for bidirectional communications: the server can send COAP Messages to that address and that is the address that is expected to be used to initiate messages from the device side as well.\r\n\r\nI'm trying to overcome this issue using a customized Agent for every request, bound to the same 5683 port where the device listens for requests originated from the server. The problem here is that this situation generates two listening handlers: the one set up in the createServer() function, and the listener for the \"response\" event from the request. The problem arises when a request is sent using port 5683, and the response arrives to the port with two listeners.\r\n\r\nThis wouldn't be a problem in case both handlers were called, as I could simply ignore answers in the server handler (e.g.: identified by the lack of method), but that's not the case: the answer to the request arrives exclusively to the server handler, thus leaving the response listener blocked forever.\r\n\r\nI know this scenario may be too specific for the Lightweight M2M protocol, but I would appreciate any idea for dealing with this problem, as I'm a bit stuck with it, including any contribution I could make to the library to support this weird case.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/mcollina/node-coap/issues/comments/74281901","html_url":"https://github.com/mcollina/node-coap/issues/39#issuecomment-74281901","issue_url":"https://api.github.com/repos/mcollina/node-coap/issues/39","id":74281901,"user":{"login":"dmoranj","id":1152892,"avatar_url":"https://avatars.githubusercontent.com/u/1152892?v=3","gravatar_id":"","url":"https://api.github.com/users/dmoranj","html_url":"https://github.com/dmoranj","followers_url":"https://api.github.com/users/dmoranj/followers","following_url":"https://api.github.com/users/dmoranj/following{/other_user}","gists_url":"https://api.github.com/users/dmoranj/gists{/gist_id}","starred_url":"https://api.github.com/users/dmoranj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmoranj/subscriptions","organizations_url":"https://api.github.com/users/dmoranj/orgs","repos_url":"https://api.github.com/users/dmoranj/repos","events_url":"https://api.github.com/users/dmoranj/events{/privacy}","received_events_url":"https://api.github.com/users/dmoranj/received_events","type":"User","site_admin":false},"created_at":"2015-02-13T16:31:59Z","updated_at":"2015-02-13T16:31:59Z","body":"Working on this problem I've realized that the specification doesn't quite specify that *all the requests from the client* should use the same port, but that *the client should listen in the same IP and port** of the registration message. This opens makes the solution far easier: I just need read the port from the outgoing socket and, once I receive the response from the registration, open a new socket to listen in that same port.\r\n\r\nI have a problem implementing this approach, though: I do not how to extract the port used in a request sent to the COAP Server from the req or res. I've managed to solve it temporary by modifying the OutgoingMessage class to add the address of the socket to the `_rsinfo` attribute, so it can be read later. What I wonder is: Am I missing a better option to extract that port? Will it be interesting to contribute that feature to the library, to help retrieving the address information, or does it have another way to retrieve it that I haven't seen?"}}