Does the Rx Framework have any use in a web application? - system.reactive

I have been looking at the Rx Framework aslo refered to as LINQ to events. This is definately a great thing but I have been wondering if anyone sees this as having any uses in web applications rather than just normal windows based apps?

You can use Rx in various call back scenarios, not just when you work 'normal windows apps'. Especially, when you work with Async operations - for ex, you might need to make a call to the server or cloud from your silverlight or desktop client and to receive the data back. Or in cases you'll get a call back from the server (in cases like Polling Duplex).
Also, another scenario for web apps - to invalidate your cache when you receive a data changed event from the model. Just some 'imaginary' code here if you've a cache and model designed accordingly...
var cacheListeners=from sender in myModel.GetDataChangedEvents()
select sender;
//Subscribe
cacheListeners.Subscribe(data=>Cache.Invalidate(data.Key));
Have a look at this http://amazedsaint.blogspot.com/2009/11/systemreactive-or-net-reactive.html

It appears that the original spark for the Rx was around web based programming - in particular to simplify the challenges of AJAX style applications.
Here's a web based example
http://blogs.msdn.com/somasegar/archive/2009/11/18/reactive-extensions-for-net-rx.aspx

See the project http://reactivetrader.com/ to see Rx in use in a web application.

Related

Showing timer with WebSockets

I have an application (Laravel + MongoDB running on Nginx) where I pull some data from the database and render it on the screen. The application focusses on multiple real life objects. Once an object is turned on (is_on equals to true in the database), the timer on the screen needs to start ticking. Once the object is turned off (is_on equals to false in the database) the clock stops ticking and resets to 0. The format of the clock is HH:MM:SS. So it shows how long the real life object is turned on.
My problem is that I don't really now how to save/implement such timer. When the user request the page, I pull the necessary data from the database. If I also save the timer in the database, you have to make a query every second which is very bad practice.
I remembered something about WebSockets and tried to look into them. I actually managed to build a basic Hello World chat application, but don't really know how to implement this in my project. There is no place for it in the database (because of the queries), so I don't really know where to save that timer on the server. I'm also doubting if WebSockets are the way to go.
So are WebSockets the way to go and if it is, can you guys point me in the right direction on how to implement this? If not, can you advise me what I should do?
Thanks in advance!
From your question:
I understand that the objects you print in the screen are modified by
users in the application, and your aim is to live forward those
modifications to other active client instances of your application.
In that case, as you mention, I would point you to websockets. They are a great way to feed information directly to the client, so the client receives the update signals and modify the interface, with no need of user action.
In order to implement the logic to notify the client, I recommend using a push approach, but this is really depending on what kind of clients you'd like to notify, since the push world is still a bit tricky.
Further readings for this websocket based push implementation:
Question about Push Flags:
Difference between push and urgent flags in TCP
If your client runs in browser or mobile this question is nice to read:
How to send push notification to web browser?
Also html5 websockets:
http://www.websocket.org/aboutwebsocket.html
As a sidenote:
A nice architecture for client-server live communication is based on node.js together with socket.io library offering good performance and not really complex implementation if you know what you do.

Testing from JUnit if a SIP Soft Phone is ringing, answered a call, disconnected

I want to initiate calls to two numbers using a 3rd party API. I need to make sure that the devices ring when they get the calls, media starts to transmit when the call is answered, and the call is terminated when hungup.
Ideally I would like to do this from JUnit tests so that I can automate this whole process. But any other tool will also be fine.
So this is what I want to do programatically,
1) Configure two SIP soft phones to answer on 2 different numbers using some credentials provided by the test.
2) Make a call using the API
3) Assert that two phones are in ringing state
4) Answer the call
5) Assert that RTP media is being transmitted among them
6) Hang up
7) Assert that the call is now successfully disconnected
I am quite new to telephony, so would appreciate any pointers on any tool or SDK that will help me accomplish this.
Please check out SipUnit https://code.google.com/p/commtesting/wiki/SipUnit.
It can do all above except check out the media is flowing yet.
Check out the KitCAT framework. It's based on JUnit and can support all of your requirements. It supports multiple user agents, which can all be coordinated within a test case. It also provides coordination with other protocols (e.g., RTP, HTTP).
Sample code:
SIPAgent callee1 = createAgent("callee1"); // sip:callee1#host
SIPAgent callee2 = createAgent("callee2"); // sip:callee2#host
// invoke your API here
invoke3rdPartyAPI(callee1.getSipURI(), callee2.getSipURI());
pause(2000);
assertThat(callee1, is(invited()));
assertThat(callee2, is(invited()));
callee1.answer();
callee2.answer();
pause(500);
assertThat(callee1, is(connectedTo(callee2))); // check for SDP match
callee1.playAudio(audioFileName);
pause(500);
assertThat(callee2, has(incomingMedia());
callee1.disconnect();
pause(500);
assertThat(callee2, is(disconnected()));
Check out the complete API here.

How do I implement chess with backbone.js with a RESTful api?

When one player makes a move that is sent to the server. And that move is pushed by the server to the second player. As far as I know, the server pushing the move to the second player goes against being a RESTful api.
From what little I know about backbone.js it is meant really for RESTful setups. Is there a way to use backbone.js with websockets to allow the server to push data down to the clients at any time?
Is there even an idiomatic way of implementing chess with backbone.js and websockets? And if not then what would be the correct way to implement chess?
You can definitely do it. Instead of fetching your collection/model, you will just set or update/reset the json data from the websocket into the proper model or collection.
Somewhat pseudo-code example:
var board = new Backbone.Collection(); // this would probably be your own extended Collection instead.
function boardChange(jsonFromServer){
// Take the json array from server,
// and update the collection with it.
// This would trigger 'change' events in each model in the collection (if changed).
board.update(jsonFromServer);
}
Implementing a chess app doesn't really require a Backbone architecture. As long as your server supports Asynchronous API, WebSockets, or even long-polling (anything real-time), it's possible. There's tons of APIs out there on the web already that does this (ie FireBase) as well as frameworks (ie, Meteor) comes into mind.
Also check out Socket IO if you're using Node JS for your server-side. There's tons of open source projects on GitHub that takes advantage of some of these web technologies already, Backbone in particular. Backbone with Socket IO. Backbone.ioBind also looks like a promising project with code samples that you can look at.
To make it work with Backbone, the data API just needs to notify any client-side listeners that an update has been made on the server which in turn triggers a change event on your Backbone Model.
You can even set a timer that performs a request to the server every n amount of time just to test out your code prototypes.
You could overload the Backbone.sync method to use websockets. The de-facto To-Do example (http://addyosmani.github.com/todomvc/) does this to use localstorage instead of a RESTful datastore, and you could do the same for web sockets. In fact if you look around Github/Google you may be able to find someone who's already done it.

Architecture sketch for iphone stock app

I am currently trying to build a (simplified) stock app (like the one built-in on the iphone). I setup a simple server with a REST-interface which my app can communicate with.
However I am struggling to find the right/best way to build this kind of (streaming data consumer) client on the iphone.
My best bet at the moment is to use a timer to regularly pull the xml payload from the server (the connection is async but the xml parsing is not therefor the interface is blocked sometimes. I am a bit shy of thread programming since I learned some lessons the hard way on other platforms).
I read about websockets but it is not clear for me if and how they are supported on the iphone.
How would you do it?
Any hint would be appreciated, Thanks.
websockets aren't going to help you -- that's a server-side technology to make a socket-like interface work over HTTP.
If you don't want to block the GUI, you need to use another thread. You are right to be scared of doing this, so share as little as possible (preferably nothing) between the two threads. Use a message passing mechanism to get information from the background thread to the UI thread.
Take a look at ActorKit: http://landonf.bikemonkey.org/code/iphone/ActorKit_Async_Messaging.20081203.html
Take a look at this question.
It talks about asynchronous vs synchronous connections. You will want to use an asynchronous call to get your data so you don't lock up your UI. You could use that in conjunction with a polling timer to get your data from the server.
You can find more info about the NSURLConnection in apple's documentation here

message queue for iOS / iPad - something like MSMQ?

I have an iPad app that works both on and offline but when I am offline there are web service calls that will need to be made once online availability is an option again.
Example:
A new client is added to the app, this needs to be sent to the web service but since we are offline we dont want to slow the user down so we let them add locally and keep going but we need to remember that that call needs to be made to the web service when we can. Same thing for placing orders and such.
Is there some sort of queue that can be setup that will fire once we have connectivity?
I don't think the overhead of a heavyweight tool like MSMQ is needed for a simple action. You can use Core Data, persist managed objects with the data needed to call the web service, and only delete each managed object after a successful post. There might or might not be a way to capture an event when connectivity starts, but you can certainly create a repeating NSTimer when the first message is queued and stop it when there are no messages in the queue.
This library handles offline persistent message queueing for situations like you describe. It says alpha from a year ago, but I have confirmed it is used in production apps:
https://github.com/gcamp/IPOfflineQueue