How does fallback work with socket.io?

Grégoire Borel

I'd like to use WebSocket with Java. Problem is, my server is separated from the client by a proxy that cannot be configured. I've been searching for implementations of WebSocket with fallbacks such as long-polling. I've found socket.io but don't know how the fallback works.

Under which case does it replace WebSocket and how?

Are there other libraries like socket.io with fallbacks implementations? I would like to find one in Java, but I only found Jetty.

EDIT: does the fallback only depend on the browser's compatibility with WebSocket? What if the cause of failure is a proxy badly configured, is socket.io going to detect it as a compatibilty failure and thus switch to long-polling (or another technique)?

Answer: since v1, socket.io includes engine.io, which brings the following features:

enter image description here

arnold

Socket.io is one of several implementations for the websockets protocol and its main selling point (IMO) is its easy of use: you don't need to code keep-alive mechanisms or decide which transport is best, it does it for you.

So, to make it clear, socket.io doesn't replace the websocket protocol, it's a package that implements it for you.

You mentioned long-polling. That is one of the transports used by socket.io. Long Polling is HTTP based and it's basically request --> wait --> response and the wait isn't very long, as it can be dropped by load balancers on EOF or stale connections. Nevertheless, it's still useful when the websockets protocol (TCP based) isn't available and socket.io automatically re-establishes the connection for you. Notice that websockets is a relatively new protocol, ratified in 2011, so older browsers don't support it. Well, socket.io detects that and then resorts to long polling, so you don't have to "worry" about it.

A websocket connection starts with HTTP, listening on the same port. For example, http://localhost:8080 (just a silly example). Then, when it's possible, socket.io switches to ws://localhost:8080 for you.

I never had problems with network topology challenges when using socket.io, as when the HTTP port is available and using long polling / websockets is possible, it just worked for me.

One of the libraries with fallback implementation, as you mentioned, is netty-socket.io. Notice how it configures the two transports:

public class Configuration {

    private ExceptionListener exceptionListener = new DefaultExceptionListener();

    private String context = "/socket.io";

    private List<Transport> transports = Arrays.asList(Transport.WEBSOCKET, Transport.POLLING);

    private int bossThreads = 0; // 0 = current_processors_amount * 2
    private int workerThreads = 0; // 0 = current_processors_amount * 2

The complete code can be found here.

Node JS has also libraries for websockets, and I mention it here just to clarify that long polling and websockets aren't the only two available transports (might be the only ones in Java):

io.set('transports', [                     // enable all transports (optional if you want flashsocket)
            'websocket'
          , 'flashsocket'
          , 'htmlfile'
          , 'xhr-polling'
          , 'jsonp-polling'
        ]);

In a nutshell, socket.io attempts to make things as easy as possible for you, including not having to worry about what transports to use, as it's done under the hood for you, yet still configurable if you want.

I hope this brief explanation helps you!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Socket.io does not work on the ubuntu 16.04

From Dev

Socket.IO not using fallback methods

From Dev

How rooms work on socket.io?

From Dev

How does HTML5 Boilerplate jQuery library fallback work?

From Dev

How does the fallback mechanism of apt update work in case of connection problems?

From Dev

How does scope work in Io?

From Dev

socket.io emit to default room does not work

From Dev

socket.io emit to default room does not work

From Dev

fallback.io doesnt work with scripts

From Dev

Socket.io and express.js, how to make it work?

From Dev

Socket.io and express.js, how to make it work?

From Dev

Fallback variadic constructor - why does this work?

From Dev

ajax success method fallback type does not work

From Dev

ajax success method fallback type does not work

From Dev

How does telepat.io work? Architecture of telepat.io

From Dev

How does socket.io get arguments from the client?

From Dev

Does Socket IO involve Disk IO?

From Dev

How does a BufferedReader interact with a Socket? About how Sockets work

From Dev

BufferedReader chained to socket, how does readLine() != null work?

From Dev

How does card.io image processing work?

From Dev

How does IO monad work in System.Random

From Dev

Can socket.io work across domains?

From Dev

Angularjs not work in socket.io response

From Dev

socket.io can't get it to work

From Dev

detect if socket.io emit did not work

From Dev

Socket.io doesn't work in production

From Dev

detect if socket.io emit did not work

From Dev

Can socket.io work across domains?

From Dev

Socket.io doesn't work in production