⚠️ This lesson is retired and might contain outdated information.

Getting started with Express and Socket.io

Mark Barton
InstructorMark Barton
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 2 months ago

Note: If you want to understand and follow along with the logging service Mark uses, watch this lesson: NodeJS Logging using Winston and Papertrail

In this lesson we will get an Express Server up and running with Socket.io. We will build the Socket.io logic in a seperate file to demonstrate how the Socket.io instance can be used within other parts of the Express Application.

This lesson is short and sweet and lays the groundwork for the other lessons in this course.

If you get an error starting the server which indicates a Papertrail connection error then you have not updated the Papertrail host and port in the logger.js.

You can safely ignore this error if you do not wish to use Papertrail - else you can sign up to a free Papertrail account and add the correct host and port.

Instructor: [00:00] 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.

[00:13] 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.

[00:35] 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.

[00:50] 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.

[01:01] 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.

[01:23] 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.

[01:32] 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.

[01:46] 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.

[02:02] 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.

[02:20] 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.

[02:46] 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.

[03:18] 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.

[03:42] 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.

egghead
egghead
~ 16 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today