What’s the best way to send data from client to server?
The example code I’m using is from How do I implement basic "Long Polling"?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, just use up the second connection to the server. This is what most frameworks do, including iirc the Bayeux protocol. If you find out you actually need that second connection, worry about it then.
Here’s some long-polling code modified from my link above:
What Moishe was talking about with the queue is that js doesn’t guarantee that xhrs will be received in the order that you dispatched them. The messages won’t be lost (or at least they haven’t been in my tests), and this isn’t a specific long-polling issue, but something to consider whenever you use xhr to send.
So here’s the queue code:
Two things to note. First, there’s a fair bit of lag if you’re sending many messages and the queue is filling up. It’s better to find a way to send the entire queue each time, rather than piece by piece. One way to do this is to convert the messages into a JSON array, and decode on the server.
Second, if there’s an error sending the message, then you’ve lost the message. There needs to be a bit of code that will either push the failed message back onto the queue, or not remove it until there’s success.