These two lessons will focus on fast client-server communication using WebSockets. We will discuss when to use WebSockets (versus AJAX) and the many applications of it.
In the working session we will build a simple chat and a bare-bones multiplayer game using the Socket.io library.
- Basically a way to send "messages" (data) back and forth between client and server.
- It establishes a persistent client-server connection, where both are constantly "listening" to each other.
- AJAX vs Websockets
- You'll use it when your app needs low-latency ("realtime") communication.
- Cases:
- your users communicate with each other
- chats
- online editing tools
- multiplayer games
- your website is constantly changing based on the user interaction
- social media feeds
- location-based apps
- online education websites
- your users communicate with each other
- WebSockets examples
- WebSockets libraries
-
Simple Chat
- Application Diagram
- Adding the socket.io library
- Messaging
- Listeners
-
Multiplayer
- Application Diagram
- Updating the server data
- Updating the client view
- Can I use...?
- JSON.stringify
- NodeJS colors in the console
Now that you have seen some use cases for WebSockets, think about what you would do with it. Try to come up with a proposal as detailed as possible. Who is it for? How does it work? What technologies — third-party APIs, mobile, NodeJS modules, etc — do you need? Does it require a database? What is the data model?