1

Is it possible to create a persistent HTTP connection using Javascript?

I would like to create a simple script that open a persistent HTTP connection to my server and print the result on a <div>. The server send string each one second so I don't want to open the connection every second...I prefer ONE connection and print the values when are available. Is it possible?

Thanks

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
Dail
  • 45
  • 1
  • 3

3 Answers3

2

it's kind-of possible. either with websockets* or workarounds (multipart responses, long polling or with the help of flash).

long polling wouldn't be wise in your situation, because it does open a new connection every second. i don't know much about multipart responses, but afaik they're only supported in firefox. for flash ... the user needs flash. and websockets are only supported by webkits (chrome, safari), firefox has them but disabled them some time ago due to a security problem (potential cache poisoning of proxies). internet explorer though ... forget it.

have a look at http://socket.io/ - it's a js library that manages workarounds for you.

stefs
  • 18,341
  • 6
  • 40
  • 47
0

The technology you are looking for is called Long Polling. It can get very complicated but it is possible.

There are several implementations. Comet is one of them, Socket.IO is another one.

http://en.wikipedia.org/wiki/Comet_(programming))

Good information also Here.

Community
  • 1
  • 1
Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
-2

It's impossible. Use

setInterval

Danzan
  • 958
  • 5
  • 8