So now we're going to start setting up some logic for our server.
So the first file that we're going to set up for our servers is our chat's file.
So let's go ahead and start adding in some of our use statements.
So we're going to use async standard task and then we're actually going to go ahead and start referencing
another type in our connection crate, which we will set up a little bit later.
And now we're going to also use an atomic reference counter, and we will start using our Tokyo broadcast
crate.
So as I said, we will implement the leaving type shortly, but we can see that we're not using Tokyo's
broadcast and broadcasts is a channel that is going to carry our messages and send them to the members
of our chat.
So the first thing we need to do is create our chats struck.
So we'll create our chat struck and say Name AAC stream and then we'll have our publisher, which is
going to be a.
Broadcast Cinder park
string as well.
So name is going to refer to the chat group and publisher uses broadcaster Cinder, which refers to
the sending side of the chats broadcast channel.
So with this struct setup, we can now implement a few methods.
The first method is going to be new, which is going to create our broadcast channel and for now we'll
just have it set to 1000 messages.
So we're going to implement this.
So we have new and it's going to contain our name, which is a reference counter to a string, and it's
going to return.
Chats.
So we'll say publisher score equals.
Broadcast.
Channel.
And then 1000 messages.
So broadcast channel is going to return a publisher and a receiver, but we don't care about the receiver.
So that's why we just went ahead and put an underscore there.
And now we want to return our
chat.
So.
As I said, we don't care for the receiver.
So that was why we used an underscore.
And now our next method is going to be called join.
So we'll have join.
It's going to have self and a atomic reference counter to a string.
And we're going to return.
Chet's.
So now we're going to create a variable called receiver equals self dot.
Publisher dot.
Subscribe and now we will spawn a task and call a sub.
Method.
Say passing in our name.
The receiver and leaving, which we passed in up there.
So now we can begin setting up our.
Sub function.
So it's going to be an asynchronous function called sub.
We're going to have a chat.
Name of string.
A mutable receiver of broadcast receiver.
An atomic reference counter.
To a string and then we will also have leaving, which is of type leaving.
And again we will implement leaving and a second.
So inside here, we're going to have a loop and we'll break down this function and a second in more
detail.
So we're going to have receiver dot receive.
Dot.
Wait.
And then OC we have a message
server.
Message.
So we need to bring our server into scope.
Server message.
Chat name.
Chat name, clone and then message.
Message got clone.
And then if we receive.
A certain air.
In this case, we're going to receive an air of type lagged.
So if our one of our one of our clients is connection is failing, then we want to be able to handle
that.
In this case, our server.
Air.
It's going to be.
Dropped X amount of messages from the chat.
Chat name.
Do not need a.
There.
And now we receive an air of.
Receive air.
Closed.
Break.
All righty.
And then if leaving Dotson, pack it away.
What is your.
Then break.
Okay.
So now let's break this function down.
We have a loop that is receiving messages from the broadcasting channel, which then transmits them
back to the client via a leaving.
Variable.
If our client is unable to keep up with the number of incoming messages so we're lagging, then it will
report back to the client the amount of dropped messages which is in.
If we received a closed air, the loop exits, which will then unsubscribe our user from the chat so
that the connection is cleaned up.
So overall, it is a pretty straightforward.
Implementation.
Once you just take a second to look at it and really break it down.
So now we have one more method that we need to implement up here in chats, and that is post.
So we're going to have a reference to our self and the message which is a type arc string and we will
say self dot publisher, dot sin.
Message.
And in this case, we do not care.
We do not care about the return value.
So that's why we put an underscore here as well.
So now that we have the chat file on the server implemented, we can start working on tracking our chat
groups.
In the next lecture.