Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observe device from an http client #850

Closed
andreafresa opened this issue Jun 11, 2020 · 13 comments
Closed

Observe device from an http client #850

andreafresa opened this issue Jun 11, 2020 · 13 comments
Labels
question Any question about leshan

Comments

@andreafresa
Copy link

Hello, I am implementing an http-client for controlling a Leshan server.
I am now trying to implement the observe functionality and I would like to know more on how you handled the multiple responses from the leshan server.
Which is the technique you used?
Cheers

@sbernard31
Copy link
Contributor

You mean a http-client for controlling a Leshan server demo ?

If I'm right, observe notification is sent by the http server demo to the browser using Server Sent Event

At server side see EventServlet.
At client side see client-controllers.js.

@sbernard31 sbernard31 added the question Any question about leshan label Jun 11, 2020
@andreafresa
Copy link
Author

andreafresa commented Jun 12, 2020

Thank you @sbernard31 .
Do you know if there is somewhere explained in details how it is handled by the server so that it can be easy to implement an http client for a leshan server?

@sbernard31
Copy link
Contributor

You need to find how to use Server Sent Event with your http client and that's all.

E.g in our javascript application demo this looks like this :

// listen for clients registration/deregistration/observe
$scope.eventsource = new EventSource('event?ep=' + $routeParams.clientId);
$scope.eventsource.addEventListener('NOTIFICATION', function(msg) {
        // Do your stuff on notification.
    }, false);

Or I didn't get your question ? 🤔

@andreafresa
Copy link
Author

Hey,
I am using an http client in python.
I am using requests. And here it is my code:
r = requests.post(url_request,"content=TLV", stream=True, allow_redirects=False, headers={'Accept': 'text/event-stream'}) client = SSEClient(r) for event in client.events(): print(json.loads(event.data))
It seems like the server close the request or the client doesn´t wait for the answer. Do you know why?

@sbernard31
Copy link
Contributor

sbernard31 commented Jun 12, 2020

I never played with this python library but my guess is that :

You should do something like :

messages = SSEClient('http://your.leshan.server.org/event?ep=your_device_endpoint_name'')
for msg in messages:
    do_something_useful(msg)

This is to listen notifications received from "your_device_endpoint_name".

This does NOT established an observe relation but just declare a kind of listener, to ask to LWM2M client to push notification to the LWM2M server, you need to send an observe request from the LWM2M server.

@andreafresa
Copy link
Author

I guess you are right. but, from what I understood the client doesn't send a request to the server for receiving update from ´http://your.leshan.server.org/event?ep=your_device_endpoint_name´.
The server just sends it.
if it is so how can you create an SSE stream?

@sbernard31
Copy link
Contributor

sbernard31 commented Jun 12, 2020

I don't get your point 🤔

Here is how it should works :

+-----------------+                                    +---------------+                                               +-------------+
| LWM2M_CLIENT_A  |                                    | LWM2M_SERVER  |                                               | HTTP_CLIENT |
+-----------------+                                    +---------------+                                               +-------------+
         |                                                     |                                                              |
         |                                                     |               listen server sent event stream for endpoint A |
         |                                                     |<-------------------------------------------------------------|
         |                                                     |                                                              |
         |                                                     |        HTTP request to observe resource /x/y/z on endpoint A |
         |                                                     |<-------------------------------------------------------------|
         |                                                     |                                                              |
         |             COAP request to observe resource /x/y/z |                                                              |
         |<----------------------------------------------------|                                                              |
         |                                                     |                                                              |
         | COAP response with current value for /x/y/z         |                                                              |
         |---------------------------------------------------->|                                                              |
         |                                                     |                                                              |
         |                                                     | HTTP response with curent value for /x/y/z on  A             |
         |                                                     |------------------------------------------------------------->|
         |                                                     |                                                              |
         | /x/y/z value changed                                |                                                              |
         |---------------------                                |                                                              |
         |                    |                                |                                                              |
         |<--------------------                                |                                                              |
         |                                                     |                                                              |
         | COAP notification for resource /x/y/z               |                                                              |
         |---------------------------------------------------->|                                                              |
         |                                                     |                                                              |
         |                                                     | send server event with new value for /x/y/z on  A            |
         |                                                     |------------------------------------------------------------->|
         |                                                     |                                                              |

@andreafresa
Copy link
Author

Maybe I am wrong, but the problem is that I am sending a request to a path and I am receiving response from another path on another http connection without a redirection or nothing.
if it is so:
1)How can the os understand which socket is related to that stream?
2)In the Client-Server paradigm, am I a Client or a Server? 🤔

@sbernard31
Copy link
Contributor

😕 Sorry but I still don't get what you mean by :

I am sending a request to a path and I am receiving response from another path on another http connection without a redirection or nothing.

@andreafresa
Copy link
Author

andreafresa commented Jun 12, 2020

Can you define me what request do you send for having Server Events? Is it a GET or a POST?

@sbernard31
Copy link
Contributor

I don't know so much about Server Sent Event.
We just used it for our demo, so I just use some API :

I don't know what the library do under the hood, if you interested you can maybe find it reading some stuff on the internet or playing with wireshark or your browser debug tools.

@sbernard31
Copy link
Contributor

See my more detailed answer at https://stackoverflow.com/a/62404442/5088764 ...

@sbernard31
Copy link
Contributor

I think we can close this issue now.
(Feel free to reopen if needed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Any question about leshan
Projects
None yet
Development

No branches or pull requests

2 participants