✔️ Learn Typescript fundamental
✔️ Work with socket and http servers
✔️ Understand sockets
✔️ Understand how transmits data
The schema below shows the architecture of the online chat:
Explanation
Each client represents an HTML page.
As soon as a client sends a message, it is transmitted as a socket to the backend.
The backend retrieves the socket and sends it to all clients as a socket.
The content of the message is then displayed on the HTML page of all clients.
We will then display the message on our frontend.
All the required information to start this workshop can be found in SETUP.md.
Be careful to finish every step before moving one to the next thing.
If you are new to Typescript, you should check our Typescript cheatsheet.
Your first step is to modify the content of your '/' routes in order to have your html page on your route.
In the file src/router/router.ts
- import path
- replace
Hello World
by your HTML page's path define insrc/Front/index.html
.
If your reload your browser page, you should see the input and button Send
.
First, you can find an express server in src/index.ts
.
You need to change this file to both use express and socket.io.
That will allow you to issue and receive data on your server.
First, import all packages required to do the step:
Then, we will add the socket server
to our express
:
- Create a new server using
http
package with your old expressapp
as arguments - Create a new
io
server that listens to your HTTP server. - Replace the
start
method of your server withlisten
💡 Your server must listen to port
8080
If you don't have any errors in your terminal, and you have your browser on your HTML page, let's go to the next step!
On every user's connection, we will log a message in our shell.
- Create an event
connection
withsocket
as parameter. - Inside you must log
[INFO] New user connected
. - Uncomment the line
express_server.app.use(express.static(path.join(__dirname, 'front'));
to connect frontend to backend.
This way you can emit and receive sockets between them.
If you open and close your browser page you should see logs on your terminal.
You have established connection !
It's time to implement a method to send messages from your frontend to your backend.
Now, complete the Class Chat
in src/front/chat.ts
:
- Create a method
emit_data
with amessage
of typestring
as parameter. - Emit socket with your event named
data
and yourmessage
. - In
index.ts
you must receive the information from thedata
event log themessage
. - Call your
emit method
inchat.ts
withHello World !
as message.
💡 Take your time to read the documentation.
In your terminal you should see Hello World !
logged.
Now that we retrieved the data, we will send it from our server to our fronted.
The objective of this step is to know when the user presses the button on the html page.
At this point you will log the content of the input into your terminal.
It's time to get information from your input.
In chat.ts
, you will create two constants variables:
form_data
: get elementform
by his id on your html page.input_data
: get elementinput
by his id on your html page.
- Make your button listen to the event
submit
. - Create a
message
variable to store the data from your input. - Log the content.
- Call
emit_data
to send themessage
. - Update input's value to an empty string.
💡 To get value from
data_input
, cast your variable toHTMLInputElement
.
⚠️ It's necessary to always update the input value with an empty string to doesn't keep the previous value.
You should see the message logged in your terminal after you have clicked the button Send
See our message on our shell is cool but see our message in our browser's console is better.
In your backend, change the way of sockets' sending to send the received message to everyone.
- Change your
socket.emit
inindex.ts
to sendsocket
to all clients in the current namespace except to the sender.
Now, if you submit a message and look to the browser's console: you should see your message.
Now we will create a new method in our Chat
class to add the content of our input into our div.
This will allow us to see the messages directly in our HTML page.
To do this, we will create a method append_data
in our class Chat
.
First, create a constant variable:
html_page
: get element by his id of youdiv
, his id should be ismessages
.
Second, in your method append_data
:
- Create a variable
add_element
that should create an elementdiv
. - Display the message gave as parameter in this new div.
- Append the
div
to your variablehtml_page
.
Finally, call this method in the emit_data
.
If you send a message, you should see it in your HTML page !!! Congratulation !
We will now retrieve the name of each user when they connect.
You will need to:
- Create a variable
my_name
that retrieves the username through a prompt that saysWhat is your name?
. - Just bellow, display
New user + name
with the username, when a user connects to the page.
We are going to emit a socket triggered when a user connects with his name as parameter.
This event will be sent to our server which will send back a socket to our frontend to display a connection message.
In chat.ts
, we will:
- Create an event
new_user
withmy_name
as parameter. - Create a listening socket named
user_connect
with astring
as a parameter. - Inside the socket, call
emit_data
with the stringConnect + name
wherename
is the parameter.
In the index.ts
we will:
- Create a listening socket named
new_user
with astring
as a parameter. - Inside it, display in your terminal
New user + name
wherename
is the parameter. - emit socket to all users with an
user-connect
event andname
as parameters.
In chat.ts
, add the name of your client before the message.
You must change the value of the print when you add a div.
When you log in, you should see New User + your_name
.
The final touch is to add the name of each user before the message is sent.
In chat.ts
, add the name of the client before the message.
- Change the value of the display when you add the content of the input into your div.
💡 Example: name: message
.
If you send messages on your HTML page you should see the username before the message.
Congratulation you have an online chat !
- Set connection and disconnect.
- Create rooms that allow you to send a message to every user that is in the same room.
- Improve your fronted with React.
- Authentication of the user with socket.
- Add a database to store messages and refresh when there is a connection.
Albert VALENTIN |
---|
🚀 Don't hesitate to follow us on our different networks, and put a star 🌟 on
PoC's
repositories.