This project has moved and is read-only. For the latest updates, please go here.

SignalR in ClearScript

Jan 25, 2015 at 3:37 AM
I am creating a Windows forms app that uses ClearScript. Users will write their own scripts to perform specific tasks to simulate a manufacturing process. The key to the app is that it must communicate with real time control systems in a lab/development environment. I created a SignalR server that provides connectivity to the real time control system and will provide javascript scaffolding that gets loaded into a V8 engine. The users then write their scripts and read write from the real time systems with little or no code on their part as I will have classes for them that greatly simplify that piece of the system.

The problem I have is that SinglaR javascript client code is based on jQuery and requires a DOM and a browser Window object - which don't exist in ClearScript.

Am I trying to mix oil and water or is there a way to skin this cat somehow?

BTW: My SingnalR server is running on a Windows 7 machine so it cannot communicate over websockets.
Coordinator
Jan 26, 2015 at 2:48 PM
Greetings!

Unfortunately many JavaScript libraries assume that they're running in a particular environment (web browser, Node.js, etc.) and are not readily usable in the sort of bare application scripting environment that ClearScript provides.

On the other hand, JavaScript is a very flexible language with dynamic typing, and depending on how much of the DOM the script requires, it might be possible to accommodate it without replicating the entire DOM. For example, ClearScriptBenchmarks mocks out just enough to accommodate SunSpider.

Is that a possibility in your scenario? What does SignalR's client script use the DOM for?

Thanks!
Jan 26, 2015 at 10:25 PM
I've not dug into the details but I don't believe SignalR needs the DOM for anything however the JavaScript SignalR client is based upon jQuery. In fact it is in the file name (jquery.signalR-2.1.1.js). And so when I try to execute jQuery followed by the SignalR script it fails because jQuery references the window object from the web API provided by the browser. I don't know what else jQuery needs but it seems to be the sticking point when it comes to SignalR without outside of a browser.

There is a Nodejs SignalR client script that does not require jQuery but it only supports the websockets transport and that doesn't work when the SignalR server is running on any Windows OS below Windows 8 or Server 2012. I actually got this part installed and working to the point that the server sent back a message saying it did not support websockets. After a little reasearch I learned about the Windows OS limitations with websockets.

I think I have found an alternate solution. I created my own .NET class that handles the client side SignalR interface. I then add this type to the V8 engine. The JavaScript then interfaces with SignalR through my .NET host environment. It was a little tricky getting all the async callbacks working but the answers you have for other discussion threads gave me what I needed to get it working. Its actually pretty cool once it gets working. Data starts pouring in very rapidly in my test environment - multiple updates every second - and it seems to be working well enough.

Right now I am just prototyping and figuring out how to solve some of these interface problems and marshalling data back and forth between ClearScript and Host. I'm nearly done with that and then I have to design the applications structure. A user will have mutiple instances of a v8Engine running at the same time and each one will have a separate SignalR connection with real-time (every half second) data updates. I'm curious to see how well it performs when it is loaded up with 10 scripts and a thousand SignalR data subscriptions. Is there anything you can think of that I should look out for?
Coordinator
Jan 27, 2015 at 2:19 PM
Hi again,

And so when I try to execute jQuery followed by the SignalR script it fails because jQuery references the window object from the web API provided by the browser.

OK, well, you could start by pre-executing a simple script such as window = this to provide a window alias for the global object. Then run the jQuery/SignalR script again to see what else it needs. If it really does need the HTML DOM, you might be out of luck, but if it only needs a tiny subset, you might be able to accommodate it by providing a handful of simple mock objects.

I think I have found an alternate solution. I created my own .NET class that handles the client side SignalR interface. I then add this type to the V8 engine. The JavaScript then interfaces with SignalR through my .NET host environment.

That sounds even better!

I'm curious to see how well it performs when it is loaded up with 10 scripts and a thousand SignalR data subscriptions. Is there anything you can think of that I should look out for?

For all performance-sensitive applications that use ClearScript with V8, we recommend that you (a) use the latest version of ClearScript (currently 5.4.0), (b) disable the GlobalMembers feature by using V8ScriptEngineFlags.DisableGlobalMembers (unless you really need it), and (c) compensate for V8's lazy garbage collector by calling ScriptEngine.CollectGarbage() once in a while (e.g., periodically or when your application is idle).

Good luck!