Fireship · @fireship

WebSockets in 100 Seconds & Beyond with Socket.io

Uploaded December 10, 2020 · Length 8:31

WebSockets in 100 Seconds & Beyond with Socket.io

Summary

Learn WebSockets in 100 seconds and beyond with Socket.io, a library that makes real-time features easier to implement.

  • websockets tutorial
  • socket.io tutorial
  • real-time features
  • node.js websockets
  • full duplex connection
  • group chat room
  • socket.io library

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    WebSockets build real-time features by establishing a two-way connection between a client and

  2. 0:05

    a server.

  3. 0:06

    Imagine an online multiplayer game where the leaderboard is constantly changing on the

  4. 0:10

    server.

  5. 0:11

    Now what's the best way to show the player an updated leaderboard?

  6. 0:14

    We could tell them to refresh their browser window every few minutes to send a new HTTP

  7. 0:18

    request.

  8. 0:19

    Or we could use an interval that ticks every few seconds to pull the server for new data.

  9. 0:23

    But these options aren't ideal for real-time data, and that's where WebSockets come in.

  10. 0:27

    The first step is for the client to send an HTTP request to the server asking to open a connection.

  11. 0:33

    If the server agrees, it will send a 101 switching protocols response, at which point the handshake is complete.

  12. 0:39

    The TCP IP connection is left open, allowing bidirectional messages to pass between the two parties with very low latency.

  13. 0:46

    The connection will stay open until one of the parties drops off, then the TCP resources can be unallocated.

  14. 0:51

    It's often referred to as a full duplex connection, which is a telecommunications term that defines how a phone line works where both parties can send messages or talk at the same time.

  15. 1:01

    Let's see this in action by creating a real-time connection between a Node.js server and a web browser.

  16. 1:06

    We'll start in our backend code by building a WebSocket server with Node.js.

  17. 1:10

    Using the WS library, we can instantiate a new server on port 8080.

  18. 1:15

    Everything is event-based, and the first event we can expect to happen is a connection from the client.

  19. 1:20

    When the connection is made, we'll have access to a WebSocket object in the callback.

  20. 1:24

    With the initial handshake done, there are two things we might want to do here.

  21. 1:27

    We can listen to incoming messages, which would just be text in this case,

  22. 1:31

    and then handle them accordingly in the callback.

  23. 1:33

    The second thing we might do is send a message back to the client,

  24. 1:36

    which we can do with the send method.

  25. 1:38

    Now let's take a look at our client-side code in the browser,

  26. 1:41

    which has a built-in WebSocket class.

  27. 1:43

    It's instantiated with a URL that points to the server,

  28. 1:46

    but notice the WS protocol.

  29. 1:48

    That will automatically trigger the handshake to open the connection, which we can listen to as an event.

  30. 1:53

    Just like on the server, we can use the socket to both listen to and send messages.

  31. 1:58

    And we now have a full duplex connection between a frontend JavaScript app and a node backend This has been WebSocket in 100 seconds but if you want to learn more make sure to stay tuned because today we going beyond 100 seconds to learn all about a library called Socket