What forms of data transfer are available to Javascript?

Asked

Viewed 424 times

10

It is common to find questions, answers and tutorials talking about AJAX, which is nothing more than the XMLHttpRequest, as a form of data transfer between two computers.

I wonder if there are other ways, be they standardized or proprietary, limited in some condition or open to any user and website. An example in a limited way, if any, would be an API restricted to extensions.

In particular, but not restricting, I’m looking for a way to transfer data - somewhere between 10 and 30 bytes - as quickly as possible between two computers within the same network. Peer-to-Peer would be ideal, but I don’t know if JS supports it. I’ve tried services like Firebase, that allowed a minimum response time, however, using a server outside the network and with occasional data loss.

Knowing other ways I wanted to find one that would fit better what I’m developing. It doesn’t seem that the answer will be anything big: the options seem to be quite limited, since it seems that only AJAX is used.

2 answers

11


There are several Apis that will allow you to carry data via Javascript.

Some will only allow you to receive data, others will allow you to send and receive.

Those that allow you to send and receive data are:

  • XMLHttpRequest
    • Requests via HTTP protocol
  • WebSocket
    • It is a cross-compatible HTTP protocol focused on faster and lighter message exchanges than per request, but does not have as efficient state resolution as XMLHttpRequest
    • Specification of the W3C
  • WebRTC
    • Protocol for Peer-to-peer data exchange, created to solve the problem of video and audio streams between browsers.
    • Specification of the W3C

If the data transmission needs to be p2p, the ideal is to use Webrtc, but it is still a bit complex to create a server that Handshake between two browsers and start communication.

A simpler solution is the use of Websockets, but all information will pass through a server that will receive the data from one client and will send it to another or other clients. But the websocket has the restriction of working with UTF-8, but it is possible to use data converters to convert the binary data to UTF-8 before they are transmitted and again to the original format when received, there are several ways, one of them is using the Uint8Array.

  • Like I said seems really very limited: ajax have to use pooling or long connections, websockets is the technology firebase uses, webrtc has worse data loss problems (compared to previous).

  • As you edit this answer, it develops: as Punchthenewbie said, I will end up having to use a Socket.IO server inside the network. With no other alternatives.

  • If you use Socket.io, it goes as long as the browser support uses the connection you can from the websocket Protocol versions until you get to pushstream and finally flash if the browser has support. Webrtc uses UDP, which will not guarantee you package delivery, you will have to work with carousel and sumcheck to ensure deliveries.

  • I’m leaving this question a little generic because it has at least two applications for me, one of them is slide show: I’m tired of passing the slide on a computer and not updating on others. So I worry about data loss.

  • Slide synchronization is very easy and efficient with websocket, in the connection event you synchronize the client with host and then just send the status changes to replicate from one to the other. http://meteor.com is a good framework for doing this quickly and efficiently. I have a demo with checkers in http://damas.gartz.com.br but it does not make the initial sync yet, only the subsequent ones, open in 2 computers and it will replicate the actions in both by websocket.

  • I’ve tried this with at least two different situations: two presentations for my teachers and two with my family (best beta testers I’ve ever seen). One used JSON pooling and the other websockets. Of them three failed and I had to manually control. The one that worked used Socket.IO. I’m looking for another way but it seems there’s no.

  • Socket.IO uses pooling if it does not have websocket support in the browser, among other fallbacks as I explained. But the problem is not in the protocol, I think it is in the implementation that you did. Because if you guarantee status on the connection and then do message exchange there is no error. Because if error occurs, it will reconnect so it will recover the status before executing the next messages.

  • Dr. I think your best chance is with Socket.IO. If you choose this implementation, be sure to give credit to my answer in this thread. This prevents future users from understanding that you used another alternative. Ab.

  • What if he uses one of several alternatives to socket.io? After all this is only the best known and most buggy of the websocket libs... has the socksjs for example that is used by meteorjs and if looking will find other alternatives.

Show 4 more comments

7

I suggest using Socket IO, a javascript extension already adapted for cross-browser use and which has an extra advantage, she chooses the most efficient way to transport data between the technologies below (which is the answer to your question):

1) Websocket;
2) Adobe Flash Socket;
3) AJAX long Polling;
4) AJAX Multipart streaming;
5) Forever iframe;
6) JSONP Polling.

Still, this extension has excellent compatibility: IE 5.5+ ; Safari 3+ ; Chrome 4+; Firefox 3+; Opera 10.61+; Iphone/Ipad Safari; Android Webkit and Webos Webkit.

http://socket.io/#how-to-use

I have had excellent experiences with this extension. When you choose a path where fallbacks are already included everything is easier!

Good Luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.