Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudFlare and socket.io

I'm using CloudFlare to implement a REST API. I need to add some notifications which are implemented with socket.io on Node.JS. When socket.io uses a live connection to keep client updated it will work because the connection is established between server-client, but what happens when socket.io does polling? Does CloudFlare use always the same server for each client?

like image 390
Pablo Yabo Avatar asked Jun 24 '16 18:06

Pablo Yabo


People also ask

Does Cloudflare allow WebSockets?

WebSocket servers in Cloudflare Workers allow you to receive messages from a client in real time.

Is Socket.IO better than WebSocket?

Key Differences between WebSocket and socket.ioIt provides the Connection over TCP, while Socket.io is a library to abstract the WebSocket connections. WebSocket doesn't have fallback options, while Socket.io supports fallback. WebSocket is the technology, while Socket.io is a library for WebSockets.

Is Socket.IO faster than HTTP?

All the frequently updated applications used WebSocket because it is faster than HTTP Connection. When we do not want to retain a connection for a particular amount of time or reuse the connection for transmitting data; An HTTP connection is slower than WebSockets.

Is Socket.IO and WebSocket same?

What Socket.IO is​ Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.


1 Answers

WebSockets are standardised by the IETF in RFC 6455, this allows you to avoid polling behaviour and instead directly maintain a bi-directional connection from the server to the client.

Whilst your existing stack (Node.js, Socket.io and CloudFlare) support polling behaviour; Socket.io can use WebSockets and CloudFlare now supports WebSockets!

WebSockets don't use traditional polling and instead act as a full-duplex communication protocol. When it is possible Socket.io will seek to use WebSockets when it can but fallback to polling when it can.

So what do you need to bear in mind? When using WebSockets be sure to use Ports that are acceptable to CloudFlare.

For requests made via HTTP/WS:

80
8080
8880
2052
2082
2086
2095

For requests made via HTTPS/WSS:

443
2053
2083
2087
2096
8443 

There is an FAQ for using CloudFlare with WebSockets.

like image 123
mjsa Avatar answered Oct 20 '22 13:10

mjsa