The socket.io provides a client-side library to abstract the browser side socket connection establishment and communication. First, we need to install this library so that we can use it in our client-side code:
- To add socket.io to our client-side script, we have to include the external script file provided by socket.io.
- To view the location and latest versions of the required script files, you can visit this page: https://cdnjs.com/libraries/socket.io.
- As the time of writing this, the location of the latest script file is at https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js.
- Add this as a script file before your main script file:
... ... ... <script
src="https://cdnjs.cloudflare.com/ajax/libs/
Chart.js/2.6.0/Chart.bundle.js"> </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/
socket.io/2.0.3/socket.io.js"> </script> <script src="script.js"></script> </body>
- To test whether socket.io was successfully installed, open your application in the browser and inspect the io variable in the console. If it exists as a function, you're good to go: