domguinard (69) [Avatar] Offline
#1
If you see the error:
TypeError: Object.observe is not a function 

Worry no more, you are not doing anything wrong! The only issue is that you are using an unsupported version of Node (see https://forums.manning.com/posts/list/38200.page). We are working on fixing that but currently you have to stick with Node version 4.X as suggested in the book.
The Observe function has been removed since Node 5 :-/

If you are unsure which version you are running do:
node --version

This should give you a 4.X version. If not then install 4.X from: https://nodejs.org/dist/latest-v4.x/. Note that you can also use the great NVM tool (see https://github.com/creationix/nvm) to install several version and switch back and forth between them.

Hope this helps, let us know if there are any more issues, we will keep you updated as of when you can use the latest version of Node.

Greg r (2) [Avatar] Offline
#2
TypeError: Object.observe is not a function: Here is a different approach
Here is my attempt at removing Object.observe from the project:

https://github.com/Greg-R/wot-book-ch7-sensor-object

This is a fork of only the Parts1-2 of Chapter 7. This section of the book is very interesting, and I have spent a lot of time looking at it and experimenting with the code and the Raspberry Pi.

It was suggested that the ES6 Proxy object would be a good substitute for the Object.observe function. I got a prototype of this working, but it did not seem to fit well within the frame work of Node.

Next, I tried converting the Temperature and Humidity sensor into a JavaScript object. ES6 provides a new "class" construct, and this also includes the capability for the class to inherit from a parent class. So I wanted the sensor object to "emit" as in asynchronous event loop which is what JavaScript and Node is famous for. So the sensor object inherits from EventEmitter and can now "emit" events and allow other JavaScript functions to subscribe to these events.

I should note that this approach only works for sensors and does not apply to the LED actuator! The actuator is still problematic with the "Observer" still required. I'm looking at Proxy again to see if it can be utilized to handle this case.

One difficult I had with the sensor class is the usage of the keyword "this" which is a convenient reference to the object itself. However, this can get "scoped" underneath an internal function, and now "this" refers to that function rather than the object. In a couple of places I had to use the "bind(this)" function to move the scope of "this" upward in the hierarchy.

There are also "arrow" functions in ES6 which may solve some of the confusion with regards to the scope of "this".

So this different approach works in my sample-of-one testing. Only the temperature/humidity sensor has been converted to an object so far.
Note that I also included a simple web page with a WebSocket connection script (websocketclient_updater.html). This has a hard-coded IP address and also the WebSocket path is hard coded to either the temperature or humidity sensor. Using this web page causes the WebSocket connection to be established, and a stream of data is sent to the web page and continually updated. It's very basic!

Thank you very much for the book! I am enjoying it and learning a lot.

Regards,
Greg

ngmwot (3) [Avatar] Offline
#3
@Gregg r nice work so far on this.

Also curious about how Johnny Five solves this. It seems Johnny Five abstracts the model a bit from the application but haven't dug in enough. The new title, "JavaScript on Things" appears to make heavy use of the Johnny Five library. http://johnny-five.io/

Any thoughts there?