Javatpoint Logo
Javatpoint Logo

How to Create a Simple Chatroom in Python

This tutorial will teach us how to create a chatroom in Python, and we will briefly understand socket programming and threading. Before diving deep into this topic, let's see the brief introduction to the chatroom.

What is a Chatroom?

Chat room is a space where multiple users across the globe can communicate with each other about their interests. They can have a conversation over any topic. It provides users with exposure to learn new things and share their thoughts. We will create a simple real-time chatroom using Python to get more understanding of the chatroom.

What is Socket Programming?

In the network terminology, a socket is an endpoint in a communication channel that bi-directional and establishes communication between server and one or more clients. The sockets are resided on the client-side and server-side associates itself with some port on the server-side. A socket should be associated with the same port for client to communicate with the same socket.

Socket programming is referred as connecting two nodes on a network to communicate with each other. The server forms the listener sockets while the client reaches out to the server.

What is Multi-Threading?

A thread is a subprocess associated with the command set, single of any other thread. So every time the user connects to the server, a separate thread is created for that user. When a user communicates from the server, a separate thread is created for that user based on the socket objects for the identity of each client.

We need to implement the two scripts to establish the chatroom. One will be the server that keeps running, and another that every client should run to connect to the server.

Implement Server-Side Socket Programming

The server takes the connection from the clients to establish a network interface. Our server-side Python script will attempt to verify the socket and bind it to an IP address and port specified by the user. Window users might have an exception for the specified port number in their firewall settings or can instead use an already open port. This script will keep running to accept the socket connection requests and append respective socket objects to the list to keep track of the active connection. Every time user wants to establish a connection with the server; a separate thread will be created for that user. In each thread, the server awaits a message and sends that message to other users currently on the chat. When the server encounters an error while receiving a message from a particular thread, it will remove that error-prone thread.

Let's start coding the server side. We created a file and named as

  • Importing Libraries

First, we will import the socket library that contains the necessary methods to implement sockets. The sys library comes with the system module which is responsible for providing data related to the system directory, functions and methods. The time module allows working with the time functions.

  • Creating the Socket and Retrieving the Hostname

The socket() function is used in the above code to create the socket connection. Once the socket is created, we extract the hostname/local name using the gethostname() function. The gethostname(), when sent with the host_name as a parameter, retrieves the other user's IP address, which is stored in socket_ip. We assign the port number 8080. We can choose this port because it is the default port on most machines. Other ports, such as 3000, 5000, etc., are commonly used for express.js. We can also run to it on a port such as '555'.

  • Binding the Host and Port

The bind() function allows us to bind the port and host together which is called on the socket object new_socket. Once we bind successfully, we print the message "Bind Successfully".

  • Listening for Connections

The listen() function takes one argument, namely numbers_of_connections. This parameter can be any number such as 1, 2, 3…..

  • Accepting the Incoming Connections

In the above code, the conn variable shows connection to the socket and the variable 'add' is assigned the IP address of the client.

  • Saving Incoming Connection Data

The incoming connection request, along with the details, is stored in the client_name variable. The client name can exceed to 1024 bytes. It is decoded on the server and prints a message that it has been connected. Then the server sends the hostname.

  • Transfer Packets/Message

The user enters the message. It is encoded using encode() function and then transfer across through the socket. The send() function is used to send the message. This function is called on the connection object created during the invocation of accept() function earlier. After that, it shows the message "the message has been delivered."

The incoming message is received using the recv() function of the conn object. It can get 1024 bytes of information. The decode() method decodes the message on the server side.

Complete Server-Side Code

Now let's see the client-side socket programming

Chatroom Client-Side Socket Programming

Now, we will create a file names as and write the code and communicate with the chat server.

  • Importing Libraries

The same library we imported at server-side.

  • Creating the Socket and Accepting User Input Hostname

Same as the server side, the socket() function is used to create the socket connection. Once the socket is created, we extract the hostname/local name using gethostname() function. The gethostname() when sent with the host_name as parameter retrieves the IP address of the other user and this IP is stored in socket_ip. We assign the port number as 8080.

  • Connecting to the Server

The detail of other server is entered first. It is important to enter the exact IP address otherwise the communication will fail. The hostname of the server and the port are bound together in a way and connected to the socket.

  • Receiving Packets/Message from the Server

The recv() function receives data and accepts 1024 of data. It is stored in the message object and decoded using the decode() function. The message is printed with the hostname of the server and the message received. The client can type any message as input, encode it, and send it to the server using the socket.

Complete Client-Side Code


In this tutorial, we have explained how we can create a simple chatroom using Python. We have discussed the terminology of the chats and how we can write the code for the client-side and server side. You can copy both codes and run them into your machine, giving you an easy interface of the chatroom on the terminal.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA