Integrating Socket.IO with ExpressJS
Socket.IO works well with ExpressJS. In fact, it's possible to run an ExpressJS application and a Socket.IO server using the same port or HTTP server.
Getting ready
In this recipe, we will see how to integrate Socket.IO with ExpressJS. You will build an ExpressJS application that will serve an HTML file containing a Socket.IO client application. Before you start, create a new package.json
file with the following content:
{ "dependencies": { "express": "4.16.3", "socket.io": "2.1.0" } }
Then, install the dependencies by opening a terminal and running:
npm install
How to do it...
Create a Socket.IO client application that will connect to the Socket.IO server, that you will build next, and display a welcome message sent by the server.
- Create a new file named
io-express-view.html
- Add the following code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Socket...