Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to send data to a specific websocket client in python

I'm writing a program with websockets in python. I've got an example server and client code running and they work well if only one client is connected. If there are multiple clients, data from the server will go randomly to one of the clients.

I would like for:

  1. Server to keep track of the various clients connected
  2. Server to be able to direct messages to a specific client out of multiple(For eg. 5) clients

websockets is the library I'm using. Python version 3.7.2

Server Code:

import asyncio
import websockets

uri='localhost'


async def response(websocket, path):

    msg = input("What do you want to send : ")
    print("message:",msg)
    await websocket.send(msg)

start_server = websockets.serve(response, uri, 5000)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

Client Code:

import asyncio
import websockets

uri="ws://localhost:5000"     
async def message():

                async with websockets.connect(uri) as socket:
                        print(await socket.recv())

while True:
        asyncio.get_event_loop().run_until_complete(message())

If I create 2 files with the client code as client1.py and client2.py, and send message from the server side, I get the sent data going to either on of the clients.

I would like to:

  1. Server keeps track of the various clients connected
  2. Server is able to direct messages to a specific client out of multiple clients

As I am just starting out with websockets, all input is appreciated.

In this output given, I intended to send all my messages to client 1, yet they got split up between client 1 and 2

like image 588
JeremyVarghese Avatar asked Jul 31 '26 14:07

JeremyVarghese


1 Answers

"websocket" targets the current connection and if you say "websocket.send(msg)" you're sending a message to the client that has just connected and websocket is an object that is reusable while the client is connected. You can assign the websocket as a variable then send a message some other time as long as the connection is still opened.

NOT RECOMMENED

Requiring user's input from the server is not a good idea because now you're awaiting the server until it receives user inputs. Nothing really happens to the server while it's waiting for user's input and this may crush your server.

RECOMMENED

A client has to tell the server which connection / client to send the message to. I would recommend using a JSON format when sending messages within client's and the server and then convert the String to a python-dict since websocket requires only strings.

Click here to check out my GitHub repository. A websockets server made only Python.

SERVER EXAMPLE

Example on how you can send a packet to a specific client

like image 113
Mane Motha Avatar answered Aug 02 '26 04:08

Mane Motha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!