I'm now creating a script on my website, which will need a bi-direction connection (the script is a chat room window). But we don't want to create a socket. Instead, we want to make client(the script) and server both have ability of sending http request to each other.
(BTW, the website server and script server are two different servers)
It is very easy for client(the script) to send http request to our server. But it is a big problem for the client(the script) to listen to http requests.
I have done some search but found nothing, maybe this requirement is so weird that seldom been used? Is it possible for a script which embeded in webpage that listen for http request?
Thanks!
I'm now creating a script on my website, which will need a bi-direction connection (the script is a chat room window). But we don't want to create a socket. Instead, we want to make client(the script) and server both have ability of sending http request to each other.
(BTW, the website server and script server are two different servers)
It is very easy for client(the script) to send http request to our server. But it is a big problem for the client(the script) to listen to http requests.
I have done some search but found nothing, maybe this requirement is so weird that seldom been used? Is it possible for a script which embeded in webpage that listen for http request?
Thanks!
- Use WebSockets and keep a persistant connection open. – Phylogenesis Commented Aug 15, 2016 at 13:24
- 1 if you dont want socket, try this html5rocks./en/tutorials/eventsource/basics – Ja9ad335h Commented Aug 15, 2016 at 13:30
- Great! That's almost what I am finding! But seems like IE/edge does not support that!? – Hubert Lin Commented Aug 15, 2016 at 13:39
- What's the reason you don't want to create a socket? – deceze ♦ Commented Aug 15, 2016 at 13:44
- That's a bit plicated. If I can, I would choose to use socket create connection. But I need a back up plan if my teammate cannot create a server which capable of creating socket. – Hubert Lin Commented Aug 15, 2016 at 14:25
1 Answer
Reset to default 4In order to receive an HTTP request, you must:
- have a publicly accessible IP address
- have an open port, publicly accessible
- bind a program to that port to listen for HTTP requests
Browsers fall down on all three counts. You cannot expect that all of your clients have a publicly reachable, unblocked IP address with specifically the port open that you want. But even if that were the case, there's no way for the browser to listen to ining requests; there's no API to do that in the browser, partly because the browser is an HTTP client and not a server, partly because offering such an API would probably provide an extremely powerful API to all sorts of attackers, and partly because it oftentimes is useless anyway because the browser cannot be reached anyway (see point 1).
So, no, you cannot turn the browser into an HTTP server.
Use WebSockets.