Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does fallback work with socket.io?

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

like image 669
Grégoire Borel Avatar asked Oct 01 '15 15:10

Grégoire Borel


People also ask

Is Socket.IO obsolete?

This package has been deprecated.

How does Socket.IO work internally?

Socket.IO allows bi-directional communication between client and server. Bi-directional communications are enabled when a client has Socket.IO in the browser, and a server has also integrated the Socket.IO package. While data can be sent in a number of forms, JSON is the simplest.

Does Socket.IO reconnect after disconnect?

Socket disconnects automatically, reconnects, and disconnects again and form a loop. #918.

Does Socket.IO auto reconnect?

In the first case, the Socket will automatically try to reconnect, after a given delay.


1 Answers

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!

like image 191
arnold Avatar answered Oct 12 '22 22:10

arnold