Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io client served from CDN

Tags:

socket.io

According to the Socket.io documentation:

A standalone build of socket.io-client is exposed automatically by the socket.io server as /socket.io/socket.io.js. Alternatively you can serve the file socket.io-client.js found at the root of this repository.

<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io('http://localhost');
    socket.on('connect', function(){
    socket.on('event', function(data){});
    socket.on('disconnect', function(){});
  });
</script>

However, I would like to serve the socket.io client from a separate CDN (it's cheaper, faster, and reduces load on my server).

How can I do this? Do I have to disable the socket.io default?

like image 802
Justin Cloud Avatar asked Mar 06 '13 03:03

Justin Cloud


People also ask

Does Socket.IO require a server?

It seems that Socket.IO is always dependent on a http server, to the point that it will create one for you, like in the example above.

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).

Does Socket.IO use WSS?

Note: You can use either https or wss (respectively, http or ws ).


Video Answer


1 Answers

As long as the version of the client you are using is the same as what you use on your server, there should not be any problem serving it from a CDN.

That said, the client is tiny (24kb), and if caching is setup properly, this should have very little impact on your server.

update: as mentioned by @maxwell2022, socket.io has its own cdn starting with 1.0.0, so you can use:

<script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>
like image 82
Pascal Belloncle Avatar answered Sep 23 '22 04:09

Pascal Belloncle