Placeholder Image

Subtitles section Play video

  • Don't you just hate people that are always on their phone all the time?

  • Well, too bad for you.

  • We're creating a chat up in this video, and it's going to be absolutely amazing before we get started.

  • Here's the final product that we're going to be creating.

  • We could go down here.

  • We can send a message.

  • Let's say, for example, high on our screen.

  • It will say you said hi and on anyone else's screen is going to say that person's name and then the message.

  • Hi, We can send a message back and again you'll see it says You said high on the screen that sent it and everyone else's screen is going to say that person's name and then the message that they sent.

  • And to do this we're using a really amazing library called socket dot io, which allows you to use Web socket, which allow real time communication from the browser to the server as well as everybody else's browsers better connected to that server.

  • So let's get started creating this now to get started.

  • I just have a really basic index dot html page with a few really basic styles that give us the styles we see over here on the right.

  • I'm not gonna cover these styles directly because they're really basic.

  • And you should style this better than this application.

  • And the first night we want to do is set apart html because it's going to be the easiest part of the application.

  • As you can see, we want a container to put all of our messages as well as an input box and ascend button for information.

  • So let's do that.

  • We're gonna credit if which is going to have an I d here, which is going to just be message container.

  • We'll build a reference this later in our JavaScript, and all of our messages that gets sent are going to be added inside this div using JavaScript.

  • Next, we're going to have the form that submits to our server.

  • We're going to give this here and I d of send container and then inside of that word is going to have an input which is going to be our text field.

  • So we'll give it a type here equal to text.

  • And then after that, we're going to give this an I D.

  • Of message input and this is just so we can again access this in our JavaScript and actually get the value from the input that we type in here.

  • And then finally, we need to submit button.

  • So we're gonna have a button which is going to the have the type of submit, and we're also going to give it here an I d of send button and lastly, inside of here, we're gonna put the text send, and there we go.

  • That's how the HTML we need.

  • And if we refresh these pages over here after we clear the goat life button, we should see that we just have a blank html page here with our container as well as our input on both of these pages that we can use.

  • And when we click, send obvious little cement information.

  • Now, in order to make this a real time chat app, we need to install a few dependencies, so get started.

  • We want to just do n p m in it, and this is going to set up our file our project to be using and PM There we go and then we want to do is we want to install some dependencies so we're going to first install socket, not Io.

  • This is going to be our real time Web socket communication.

  • And then we're going to install node mon so we can do N p m I and this one.

  • We want to be a deaf dependency.

  • So you make sure you put save Dev.

  • Spell that quickly and we type out note money and Newman is going to allow our server to automatically refresh itself every time we make a change.

  • So we don't have to cancel our server and restart our surgery now instead of our package dot Jason, As soon as that finishes up, we can actually create a script here that we're going to use to start our server.

  • And what, you gonna call this?

  • A Deb start.

  • And all we want to do is run.

  • No Goodman and we want to run it on our server file.

  • So called server dot Js is going to be our server file name Now what?

  • That Out of the way.

  • Let's actually create that server dot Js And there we go.

  • And now if we run in P m.

  • Run, Dev start, that's gonna run node mont honor service Js file.

  • And as you can see, that work just fine.

  • Now to create our server using socket.

  • I was actually really easy.

  • We could just include our soccer die.

  • Oh, so we're gonna say constant Io is going to be equal to require socket dot io and in order to actually credits over, all we do is pass the port that we want to serve it to run on to this socket I will require as a function.

  • And this is going to create a server for us on that port 3000.

  • Then what we can do is we can just use our io.

  • We can say I owe dawn connection on what this is going to do is every time a user loads up our website, it's going to call this function, and it's going to give each one of these users their own socket.

  • And what we can do with this socket is send a message down to the user so we can just say socket dot Aim it and we can put whatever we want inside of this.

  • This is going to be the event name.

  • We're just going to call this chat message and then we passed the parameters or data that we want And this data could be whatever you want it to be on our case, we're just gonna make it a string Hold Hello, world.

  • So now every time someone connects to our server, we're sending a message down to the client with this event chat message, and it's going to say hello world inside of it.

  • And now you notice we have an error.

  • We need to have this function inside of here.

  • So just move that princey around their correct voice.

  • Save it and everything will be deep green down here.

  • And now let's create a separate file here, which is going to be script dot Js.

  • This is going to be where all of our client's side javascript is going to go.

  • And the nice thing about using socket Io in this way where we set it up on Port 3000 and we have our server being run on Port 5500 for our client is that our client and server are completely separated from each other, and we don't have to worry about them being inner mingled together.

  • They're completely separate entities.

  • You could host them to different places if you really wanted to, and they would still be able to communicate just fine.

  • So what we need to do inside of this script dot Js is we need to get the socket variable and do that.

  • We could just say constant socket.

  • We want to set that equal to Io and the location of where we're hosting our socket.

  • And this is just going to be at http colon Backslash backslash loco host 3000.

  • And this is where our server is hosting our soccer Js application.

  • Next, What we want to do is we want to say socket dot on and remember used our message of chat message.

  • This is our event name.

  • So whenever we receive an event, we want to call a function inside of here with the data that we sent down from the server.

  • And we're just gonna for now, Consul, about long that information as data and again, this is going to be hello world because that's what we have instead of our service Js here next inside of her index at h T mo.

  • We actually to require these scripts.

  • So first we're going to require a script for Socket I owe.

  • So we can just say script here.

  • We wanted to for this, And the source is just going to be set to http.

  • Backslash backslash again.

  • Local host 3000.

  • Because this is where our socket io is working.

  • And with our library socket, I Oh, it's going to expose path called socket dot io, and it's going to have a file in there called socket dot io slash or sorry dot Js And this is going to be the Java script file that includes this right here.

  • This Iot function.

  • Next.

  • We want to just include the script that we created so we can do another script tag.

  • Defer this and we're gonna set this source equal to script dot Js Just like that.

  • Close that off.

  • And now if we inspect one of these pages over here, you'll see we get that hello world message showing up in our council.

  • And that's because when we connected, our server went inside of the socket here.

  • We could even just log inside of here.

  • Whoops, counsel dot Log.

  • We just want to say new user.

  • Save that and now you can see as soon as we connect, we get this new user message and then it's sending hello World down to our client.

  • We're receiving that inside of our socket dot on and then logging, not in the council.

  • And the reason we're getting to New Year's or messages just cause we have two windows up.

  • And so both of these users are connecting in the same exact time.

  • Now it's closed out of that and actually work on making this a chat application where the users can communicate back and forth now on the client.

  • What we want to do is we want to get our message form.

  • So essentially, every time we send a message, we want to send that to the server instead of sending it through the form here.

  • So let's get a variable, which is going to be our form.

  • We're gonna call this message form.

  • This is going to be equal to document hopes document get element by i.

  • D.

  • And we gave that a d here of send container.

  • So this right here message form is going to be our form and then we want to add an event listener to that so well said message form not add event listener and it's just going to be whenever we submit our form.

  • What we want to do is we want to stop our form some of submitting.

  • So to do that, we can just say ee dot prevent default is going to stop our page from actually posting to our server, which is also going to stop it from refreshing over here.

  • And if we say that in click send, you'll see our page does not actually refresh, which is perfect.

  • If we didn't do this, we would lose all of our chat messages, which we do not want next.

  • After that, we want to actually get our message.

  • So we want our message is going to be equal to hear.

  • First, we need to get our message element.

  • So we're just going to say up here, we're gonna get our message input, which is equal to document hopes Document that get element by I.

  • D.

  • Is going to have an I d.

  • Of message input.

  • Now that we have that message element, we could take that message element.

  • Sorry.

  • Input on what we want to do is just get the value from there and then we actually want to send that value to the server so we could just say socket close value, socket dot admit and admit is going to just send information from the client to the server and I'm going to do is given an event name.

  • So we're gonna call this send chat message, and then what we want to do is send it that message, which is just the value here.

  • And then the last thing we want to do is just take our message input, and we want to set the value of that message input equal to a blank string just so it empties out the message every time we send it.

  • Now, if we types underling here click send, it's going to clear it out and send it to our server.

  • But our servers actually not handling this yet, So we need to set up our handling for that event in order to do that.

  • It's really straightforward.

  • All we have to do inside of here is we just say socket dot on just like we did on the client.

  • And we say the event, which in our case is send chat message and then it's going to get that data that we sent with it, which in our case, is just the message itself.

  • And now inside of here.

  • What we want to do is whatever we want.

  • So we'll just logged this for now to show that it's actually working.

  • We're gonna log the message, remove this other long, and if we save that and we send a message of Hello, click send.

  • You'll see that shows up in a consul down here because we're actually receiving that on our server.

  • But what if we want to send that down to the other client?

  • Well, that's really easy to do.

  • Always say is socket dot admit.

  • I'm sorry dot broadcast dot Aim it.

  • And this is going to send that message toe every single other person connected to the server except for the person that sent the message So it won't send it back to our original user, but it'll send it to all the other users, which is ideally what we want.

  • And inside of here, we could just send chat message on Beacon.

  • Send down that message, must remove this initial innit?

  • And instead of here, we're going to inspect this council.

  • So we have our consul right here.

  • And if we typing a message here, for example Hello and we hit send.

  • You'll see that gets sent down to our client over here, But it does not get sent to this client, which is perfect.

  • We only want to get to sent to all the other clients and not the person that originally sent that message.

  • And now that we have that hooked up, let's actually start a pending these messages to our actual index file.

  • Now, we go back into our script here, and this is going to be really easy.

  • We're gonna create a function for doing this, which is just going to be called upend message, and it's going to take a message.

  • And in here we're just going to create an element.

  • We're just gonna call message element, and you could just do documents dot create elements and we want to just create a diff.

  • So just type Devyn here is the element that we want to create, and then we want to set the value for this.

  • So relations, same message element dot in her text is going to be equal to our message, and then we just want to upend that toward container.

  • So again appear we can get our message container.

  • And this message container has the idea of message container.

  • And now down here, we can actually take that message container so and we could do upend, which is just going to add whatever element we want, which in our case, is message element.

  • This is going to be added to the end of our container.

  • And now we can call that a penned a message message right here, Senate in the data, which in our case, is just our message.

  • Now, if we save that, send something on this server and click send, you see it's going to go over the other client and show up that message for them.

  • Now, the next thing that we want to work on is giving our user names to display, because right now nobody has a name.

  • So in order to do this, we're gonna use which just the prompt element.

  • So we could just say constant name is going to be equal to prompt.

  • And then we asked them, What is your name?

  • But what is your name?

  • And now when we save our screen, you'll see that we get our what is your name, message?

  • Pop up and we can type in whatever your name is and that'll be populated into this variable name here and now.

  • After we're done with that, we just wanted a pen, that message to our screen so we could just depend the message of you joined because we just want this to always say you when it's the person that's actually sending it.

  • And now you can see when when we type in a name over here, it's going to say you joined, which is perfect.

  • Now we also want to send that message to our server.

  • So we're going to say, socket dot admit And we want to admit whenever we get a new user signed up and we're going to send that name along with this message now inside of our server over here, we want to actually catch that.

  • So we'll say socket dot on new user.

  • And this new user is going to be taken, of course, a function that's going to have the name as the data and now we need actually store this user information so it's creative variable appear, which is going to be called users, and we just sent this to an empty object.

  • What we want to do is we want the key of this object to be the idea of the socket.

  • So we'll say users of socket that I d and all these sockets have a unique I d which we can use.

  • So that's why we can use that is the key here.

  • We want to set that equal to the user's name.

  • Then once that's complete, we actually want to send the message saying, This person connected to the rest of our users.

  • So we're going to do another broadcast will say sokka dot broadcast dot admit on.

  • We want to admit a user connected message so we could just pass here the user's name.

  • And now we need to handle this on our client.

  • So let's go down to our client here, and we could just copy this essentially exactly the same.

  • And instead of chat message, we want to say, user connected, and this is going to be the name of the user insect, the data and we want to upend a message here, which is just going to say the person's name.

  • So we can just come in here.

  • We can say name, and we just want to say that they connected.

  • Now save that.

  • We can type in any name here and if we quick Okay, what's one more time?

  • Click Okay, you can see up here says you joined.

  • And on the other person's screen it says that person's name connected, which is perfect now.

  • One thing that does happen, though, is if we send a message is still comes across without the user's name.

  • So we want to include the user's name in this chat message event back on our server.

  • We can do that really easily by broadcasting here an object instead of just a single message.

  • We want the key to be message for our first element, and then we want to use the user's name as the second element.

  • So we can just say users socket dot i d.

  • And this is going to get the name of the user with that particular i D.

  • That sent the message.

  • And now we're sending both the message as well as the user's name that sent that message down to the server or down to the client.

  • I mean and then in here for a pending our message.

  • We have our data, and what we can do is we can first get the user's name so we can just say data dot name.

  • And then we can get the message after that by just saying data that message.

  • And if we say that, what's type in a name here will say, Jim, let's do that one more time.

  • There we go and we can just say hi.

  • And now you can see over here It says Jim connected, and then Jim High, which is perfect.

  • Now we want to actually show that message on the user screen that sent it because we need to have them be able to know which messages they send, and that is going to be really easy.

  • We could just call our upend a message method inside of here, where we actually are adding the event.

  • And instead of using data name, we just want to say you and we want to make sure that we have penned the message right here.

  • You know, get this dollar sign and we save that.

  • We could just type in here name.

  • And now if we send a message click Send it.

  • You see right here it says you and then your message that you sent, which is perfect.

  • Now the very last thing we have left the handle is what happens when a user disconnects from the server.

  • We want to send a message just like the joint message.

  • But we wanted to say disconnect instead.

  • So run our server.

  • Let's set that up.

  • We're gonna have a socket here that is going to be saying, Is that a new user?

  • We want this one to say disconnect.

  • And this disconnect is not gonna take any parameters because we don't actually care who disconnects.

  • We just care about their socket i d and what we can do down here as we can broadcast to the users which one's disconnected so we can say it broadcast.

  • He met here.

  • We want to send the message of user disconnected and then inside of here.

  • We want to send the user's name, which is just going to be users and then sock it that i d.

  • This will get the idea.

  • The socket and the user's name is linked to that value.

  • Then we just want to delete that user from the array of objects.

  • So to do that, would you just say delete?

  • Oops, delete.

  • And we want to delete the element at this key.

  • Now, Doing that is going to remove the element from the users are right here and send it down to the users saying that user disconnected So we can handle that down here.

  • Very similarly to how we do our normal connect.

  • We can just do disconnected.

  • And instead of saying they connected, we can say that they disconnected.

  • Now, let's type in a name here of Jim, for example.

  • What's one more time?

  • Okay, you can see it says Jim connected down here.

  • And if I go over here and I actually just closed out of this, you'll see we get Jim disconnected down here.

  • I'll just go back to global host so we can open this back up with Sarah.

  • Name is Kyle.

  • Now.

  • We see we get Kyle connected down here, and that's all it takes to build a real time chat application.

  • If you want to see me build a more complex version of this project, make sure to let me know down in the comments below, because I'd love to expand on it further.

  • Also, if you enjoyed the video, make sure you check on my other videos.

  • Those are linked over here and subscribe to the channel for more videos just like this.

  • Thank you very much for watching and have a good day.

Don't you just hate people that are always on their phone all the time?

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it