Chatty Chat: Part 8 - Get started with socket.io

JavaScript API
Transcript

English (Auto-generated)

Hi everyone. We're now going to start using the socket dot iO library, which allows us to create bidirectional communication between the client or clients and a silver. So essentially we're working on the text chatting part in the upcoming cast. Okay, so socket dot io is a javascript library and it enables bilateral communication, which is real time and synchronous communication between the client side and silver. And this library is built upon the web sockets. A P I so socket dot IO has two parts a client side library in the browser and number two a silver side library for node. So when you're using socket dot IO, you're playing a ball with two files at the same time. For example, socket dot Js here or chat dot Js, which you know, is client side. And then we have index dot Js, which is the silver side javascript file. And this is because you must write the code or logic to send a message from either the silver or client and then receive it at the other side, which again could be a client or silver. So let's go ahead. And if you haven't already install the library with NPM, install socket. God, I'll okay, and I'll just start my silver as well. Right, okay, now let's include it in the index dot Js file where we have our silver code running. So out here will require it. So let's say um let socket is equal to require and then we want to require a socket dot io. And basically, if you go to package dot jayson, you'll see it's out here. So we're requiring socket dot i O. Right. And after that lead, silver is equal to then require http and dot silver. And let's run our app. So we're requiring the HTTP module and then creating a silver for our express app. And we're passing uh this to the variable server. Right. And or we're assigning it to the variable silver. And then we have led IO is equal to require, let's require basically um we're going to we don't need this. So let's uh let this be so I'll say let IO is equal to require um socket dot io. And what I'm going to do out here is I'm just going to do this Silver. Right. And this is going to be above this app dot use a line. Right. So this creates a new socket dot io instance attached to the http Silver. Essentially. Now socket connections will be listened for on this Silver. And then let's change this line of code at the bottom from app dot listen, two server dot listen, so we're listening for socket connections on a silver. And this part is important else you won't be able to see any of the messages which are being exchanged between the client and the silver. So control s to save. Now, remember that we also need to set up socket dot io at the front end. And basically that's already being done by including it in room dot html where people are going to be um, chatting. So we've included or we've imported the socket library from the node modules folder. And here it is. Okay. And included in this project folder for this cast is the socket dot Js file. So if you don't have it, make a new file called socket dot Js under the public folder where we're going to write our javascript code for the chat room on the client side. So now that we've got our dependent dependencies installed and included, we are now next going to be working on making a socket connection.
129 Views 0 Likes 0 Comments

Let's get set up to start using socket.io which establishes bi-directional communication between a client/clients and server.

Comment
Leave a comment (supports markdown format)