views:

122

answers:

3

Hi All,

I'm making an application with server sided variables that change every second. Every second those new variable need to be shown at all the clients that have the webpage open.

Now most people told me to go with comet because I need to push/pull the data every second, now I've got a few questions:

  • What would be a better solution looking at the fact that I need the new data EVERY SECOND, pulling from the client or pushing with the server?

  • Also the item ID's that are on the server side (with the variable's that ID got) can change and when the client refreshes the page he needs to get the oldest (and living) ID's. This would mean that my jquery/javascript on the client side must know which ID's he got on the page, what is best way to do this?

  • Last thing is that I can't find a good (not to expensive) comet library/api for asp.net (C#). Anyone ever used a comet library with good results? We're looking at a site that should be able to have 2000 comet connections at every moment.

+1  A: 

What would be a better solution looking at the fact that I need the new data EVERY SECOND, pulling from the client or pushing with the server?

I don't think it doesn't matter that much, as the time between requests and the time new data will be available is rather short. I would just instantiate a new XMLHttpRequest at the client after the previous one succeeded. You could send the server the last received data (if not too big) so it can compare that data with the current one available on the server and only send something back when new data is available.

Also the item ID's that are on the server side (with the variable's that ID got) can change and when the client refreshes the page he needs to get the oldest (and living) ID's. This would mean that my jquery/javascript on the client side must know which ID's he got on the page, what is best way to do this?

I'm not totally sure I understand what you mean, but if I'm right you can just store every name/value pair in an object. When a new variable arrives at the client, it doesn't overwrite existing data; when a certain variable is already present, it is updated with the latest value. It could look like:

{ first_variable: 345,
  second_one: "foo",
  and_the_third: ["I", "am", "an", "array,", "hooray!"]
}

and when a new state of second_one arrives, e.g. "bar", the object is updated to:

{ first_variable: 345,
  second_one: "bar",
  and_the_third: ["I", "am", "an", "array,", "hooray!"]
}

Last thing is that I can't find a good (not to expensive) comet library/api for asp.net (C#). Anyone ever used a comet library with good results?

I don't have any experience with ASP.NET, but do you need such a library for this? Can't you just program the server-side code yourself, which, as I said, leaves the connection open and periodically (continually) compares the current state with the previous sent state?


UPDATE: To show it's not that difficult to keep a connection open at the server side, I'll show you a long-polling simulation I wrote in PHP:

<?php
  sleep(5);
?>
<b>OK!</b>

Instead of letting the process sleep a few seconds, you can easily test for changes of the state in a loop. And instead of sending an arbitrary HTML element, you can send the data back, e.g. in JSON notation. I can't imagine it would be that hard to do this in ASP.NET/C#.

Marcel Korpel
I did find some good tutorials on the last thing you mention, keeping connection open en receieve send http requests. Now the problem is that I don't know how fast it's going to be if I write every part myself. I'm going to try it later today anyway but may take a long time. Also I've got like 4 standard div's which should get an ID when the page loads. After this every second it updates it looks at the ID of the div and puts the right value there. It's much like those penny auctions you see more now-a-days. Anyway thanks for your response! Hopefully more people react.
Julian
@Julian – I'm still not sure I understand you correctly. Manipulating the DOM in JavaScript is very easy nowadays. If necessary you can always ask new questions, but many questions already exist here on SO. And I don't attend penny auctions in our country. ;)
Marcel Korpel
Yeah, maybe it's just me. I'm normally a C# winform developer and not really into the html/javascript stuff yet. But the application we need to build is not like a penny auction, but you can think of the same kind of update every second.
Julian
@Julian – BTW, if it has to be really precise (e.g., it is important that all clients update their screens at the same moment), then forget it. HTTP was never designed to be used that way.
Marcel Korpel
Hmmm.. at the moment I'm also looking at the ajax/json solution with a webservice. If you write it yourself with jquery and don't use the normal ajax build in stuff it should be pretty quickly. I've been looking into those penny auction sites and they also send json messages every second, at least it looks that way with firebug.
Julian
@Julian – Please mind that jQuery is merely a framework over JavaScript, you're still using the “normal ajax build in stuff” (XMLHttpRequests). And also have a look at [meta's chat](http://chat.meta.stackoverflow.com/), which does the same as those sites you refer to.
Marcel Korpel
+2  A: 

WebSync by Frozen Mountain is a full-fledged scalable comet server for IIS and ASP.NET. It integrates seamlessly into your application stack to support real-time data communications with thousands of clients per server node.

Check it out, there's a Community edition freely available.

Anton
A: 

There is a SendToAll function in PokeIn ASP.NET ajax library.

BigbangO