WebSocket@Python

sonu kushwaha
3 min readMay 16, 2021

--

When building a real-time application, there’s a moment where you have to choose how to implement the real-time data exchange between client and server. WebSockets is probably the most popular solutions for implementing real-time communications in the modern web.

WebSockets

When talking about WebSockets, we refer to a web communication protocol that provides a full-duplex communication channel over a single TCP connection. In few words, what it does is, it allows interactions between client and server with minimum overhead, allowing us to build applications that use the advantages of real-time communications.

For the time being, imagine if you’re building a chat app: you need to receive and send data as soon as possible, right? Well, that’s the right job for WebSockets! You can open one TCP connection and share data leaving it open as long as you need it.

WebSockets first appeared in 2010 in Google Chrome 4, and the first RFC (RFC 6455) has been published one year later, in 2011.

Great use cases for WebSockets includes:

  • Chat Applications
  • Multiplayer Games
  • Collaborative Editing
  • Social Feeds
  • Location-based Applications

and many more.

Now, here in this blog I will be showing lite chat app ,ie in python where server will be running on RED_HAT_LINUX_8 and in this server side have the concept of WebSocket and Multi-threading to achieve lite chat application

server side program for chat application in python

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) OR s=socket.socket()

ip=””
port=1234
s.bind((ip,port))

the above line of code,talks about the type of socket family used here (IPv4) ,and how is the data going to be transferred via TCP or UDP here TCP is used as to provides a full-duplex communication channel over a single TCP connection. now bind part binds the IPv4 and port number that forms the socket, socket is the complete IPv4 along with PORT number for that particular process like 192.168.254.17:1234 is in above case)

192.168.254.17:1234 is in above case ,with process id 3398

s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1

s.listen()

this piece of code will hold the established connection in listening state, as one could see in the above screen shot

while true:
c,addr=s.accept()
t1=threading.Thread(target=iiec , args=(c,addr))
t1.start()

def iiec(c,addr):
print(addr)
c.send(b”i am from server@sonu”)
data=c.recv(1024)
print(data)

this block of code will ,accept the incoming request from client side,and here the concept of multithreading is used to establish the more that one connection can be established at a single point of time {t1=threading.Thread(target=iiec , args=(c,addr))}
now here we come to the function called iiec which uses the argument as c and addr as c is for connection and address is for the address of the client respectively and then data is exchanged b/w the server and client side

client side program ,with the message received from server side
server side program ,with the message received from client side

The plain WebSockets, which need to run just two requests/traffic by the help of browser or Linux operating system(RedHat Linux8 used):

  • The GET request for the client side
  • The UPGRADE connection to WebSocket

The Above code are the glimps, every modern browser supports WebSockets these days.

For instance, if you’re writing a chat application and you want to notify all the connected clients that a new user has joined the chat, you can easily broadcast that message in one shot to everyone. Using plain WebSocket, you’ll need a list of all the connected clients and then send the message directly one by one.

for source code visit:- https://github.com/sonukushwaha403/python_socket-TCP.git

--

--