Instructor: The Socket.IO framework comes in two parts, a Node.js server and a client-side library. In this lesson, we're going to get our Node.js server set up for working with Socket.IO.
We're going to get our packages installed for our Node application. We're going to start with Express. Express we're going to use because it's a well-understood framework. We need to host some static files. Also, in later lessons, we're going to be triggering or simulating the triggering of an external system which then needs to send messages to connected clients.
We have the Socket.IO package itself which is for the server but also comes bundled with the client library. We're going to be using dotenv because for best practice for Node applications we're going to be hosting our configuration in an external variables file. I have a lesson about that which I'll link below.
We're also going to use Winston for logging. Again, I have another lesson to talk about Winston and Winston Papertrail. We're going to be doing a lot of logging as information is flowing between the client and the server.
The first thing we're going to need to do is create our server.js file which is going to be used in the actual Express application. Within the application, the first thing we're going to put in is our reference to the variables file. The variables file is our local configuration which is loaded at runtime. We're just setting our Node environment to local here and a port.
I've copied in some boilerplate Express code, so I'm just going to run through it very quickly. We've got a custom logger, which I mentioned previously. We have a reference to Express.
The port that we're going to run on, which is 8,500. This is coming from the variables file. We're going to be using Express as our static file hosting server, so we've got a reference to the Express static method there. Finally, the actual Express server is started.
Below here we have our custom logger being used to output the fact the server has started and what port it's running on. If you run the server now, it should run fine. Our server started successfully.
Now we could start to get Socket.IO working with our Express server. The first thing we need to do is get a handle on the Node HTTP server, and we can do that by first requiring the HTTP module, calling the server method, and passing the Express object.
To make the Socket.IO functionality available to be used throughout our Express application, the best thing to do is create it in its own file. We're going to create a new file called IO. First, we'll require our Socket.IO package, then we're going to make an object at the top called IO. This is what we're going to be returning and available to the rest of our Express application.
We need to create a new method which we're going to export called initialize. It's going to be passed the Node HTTP server and we instantiate our Socket.IO object by using the Socket.IO package and pass it that HTTP server. Finally, to make this IO object available we're going to export it. You'll see in future lessons where this becomes useful.
Now what we need to do is get a handle on the IO object in our server.js file. Going into our server.js file, I'm going to set a new variable called IO. That's going to be set to our IO file. We're going to call the initialize method and pass it the HTTP server.
That should be it for setting up our Express server. In the next lesson, we can crack on and actually start using the Socket.IO functionality. I'm just going to fire up our server to make sure it runs OK. Yep, no errors. We can move on.