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?
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.
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).
Note: You can use either https or wss (respectively, http or ws ).
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With