In the receive function, the keyword ‘NICKNAME’ is sent to the clients which means their nickname is requested. We’ve done a lot of work but, adding the clients still awaits. It first tries if a message can be received from the client’s end, if yes, it is broadcasted.But if there’s any kind of error/issue, the server keeps things simple. Handling the clients is a pretty tough job, so is the function. We are going to use it ahead in other places too. Its basic function is to send message to the clients in the clients list. Then we move on by defining the broadcast function. First one indicates the usage of internet socket and the other one indicates the usage of TCP. One needs to know that only unreserved ports can be used as this is going to work on LocalHost and the computer may cause problems if you use the reserved ones.While defining socket, two parameters named (AF_INET) and (SOCK_STREAM) will be used. Then we’ll move on to define our IP and port. Socket programming is a way of connecting two nodes on a network to communicate with each other whereas the threading module provides a very simple and intuitive API for spawning multiple threads in a program.
Both are built-in libraries so there’s no need to pip install them. Server-side code:įirstly, we’ll be importing two libraries named ‘socket’ and ‘threading’. There will be no direct communication between them.
One thing that needs to be very clear is that clients will talk only via server. Getting Started:īefore diving into code, the last thing we want to know is to follow this architecture, we need to write two scripts, one for the server-side and the other one for the client-side. It means multiple clients will be hosted by one server. Architectureįor chat room, we’re going to use the server-client architecture. In this tutorial, we’re going to build a group chat room that can host more than two clients at a time. It can be interpersonal (one-one) and group chat too. What is a chat room?Ī chat room is a medium/interface that allows two or more people to chat and send messages to everyone. Here is an example of how you can build a simple command-line based chat room using python which is easy to implement and understand. Python is one of the most versatile programming languages and one can observe that through its various applications everywhere.