Sunday, June 12, 2005

Client-side Business Objects?

There's a lot of buzz around AJAX these days, and I started thinking about how this type of client-side code could be put to alternative use.

I was recently asked about how to interface a web page with code that reads from the PC's serial port. For example, if the PC is connected to a scale or other sensor via the serial port, and this data should automatically be available to a form on the web page.

Of course, the traditional "Use an ActiveX Control" solution came to mind, especially since I've created a control for this exact purpose in the past. Similarly, a UserControl written in .NET can be hosted by IE (after playing with CAS policies). But, then AJAX came to mind, and I thought of this solution:

  1. A native application runs on the client (perhaps as a service, or a console app, windows app, etc). Can be VB6, C/C++, .NET, Java, or whatever, so long as it can perform the tasks that follow.
  2. The native app is able to read and/or write to the serial port as required. Much of this code probably already exists if the developer was prototyping the communications code.
  3. The native app includes a HTTP listener. (I would lean towards .NET, since it's so easy to create and use HTTP listeners).
  4. The web page includes javascript code that does AJAX-like communications between the local "webserver" and the browser.
  5. Upon receiving a request via the HTTP listener, the native app does the serial port comminications necessary to retrieve the results, and then packages it up and sends it to the browser via the response object.

The beauty of this approach is that so much of the "Business Logic" can be written in the native/compiled code, which lets you do far more than what you can do with Javascript alone (image generation/processing anyone?). Also, the AJAX-like code doesn't have to be very long or very complex to interact with the native code, the browser is not limited to Internet Explorer, and the "server" is local, so network communications are reduced.

This was a simplified example of what I feel could become a cool architecture. If security issues could be overcome (nothing that ActiveX and Java Applets haven't struggled with, such as controlling permissions to filesystem and peripherals, etc), then custom business logic could be packaged as simple modules that plug into a master "listener".

This might actually fit well into my Personal Portal Server concept.