Python Forum
trying to stream data from a websocket to client
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to stream data from a websocket to client
#1
My mission is to create a websocket server that send signal to any client connected to the websocket, the signal should be sent only when they is a specific event.

1. So I have a function called calc_totential_buys(), this function is what is returning the signal that i want to send to the client via a websocket.
If the signal returned from calc_totential_buys() is None then that is considered as no event and I only want to send to the client when they is an event

async def calc_totential_buys():
  # This code is not my actual code, it is just for demostration purpose
  database_return_value = mydb_data()
  
  if database_return_value != []:
    return database_return_value
  else:
    return None
2. then I also have my server side websocket code
the verify_user() function is a function that takes in a user unique license key and the computer serial number as machine_id as it params
and it will check the database to very if the client that is connected to the websocket is whitelisted by sending a dict of some values to the client machine and the client websocket side will handle the verification

import json, websockets, asyncio

wsssets = set()

async def echo(wss, path):
    wsssets.add(wss)
    try:     
        async for message in wss:
            if "lin=" in message:
                x = message.replace("lin=","").split("-")
                license_key = x[0]
                machine_id = x[1]
                resp_msg = verify_user(license_key, machine_id, mydb)
                await wss.send(json.dumps(resp_msg))
            else:
                # here will be the code to handle a message the the client send to the server in ocasion
                # so that i will know that the last time the client was active
                pass
   
    except websockets.exceptions.ConnectionClosed as err:
        print(err)
     
start_server = websockets.serve(echo, "localhost", PORT,)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
3. Now the problem I'm facing is:
  • If the client is connecting for the first time then the verify_user() function is been call sucessfully and it return the data from the database as needed, but if the same client is connecting for the second time or another client is connecting to the websocket then verify_user() function will still return the same value that it returned on first initialization, meaning it does not fetch the data from the database anymore untill i restart the server websocket.

  • I dont even know where and how to call calc_totential_buys() function in the echo() function to send the signal whenever the return value is not None beause the echo() function is runing constantly on where it is looping to check for the connected client message and if I try to call the calc_totential_buys() and use if statement to check if the value is not None then send to the client a message then that line of code will not be reached.
Reply
#2
I now realease that i should make admin client that will send message to the server and ther server broadcast it to other connected clients
Reply
#3
WebSocket servers often send the same message to all connected clients or to a subset of clients for which the message is relevant. Let's explore options for broadcasting a message, explain the design of broadcast() , and discuss alternatives.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  websocket help johnbob 0 939 Jan-13-2024, 06:32 AM
Last Post: johnbob
  Socket to stream data and tranmit and revieve data kiyoshi7 0 2,375 Aug-11-2022, 10:52 PM
Last Post: kiyoshi7
  Help with Websocket-Client Module - Infinite Loop CluelessChris 0 3,943 Apr-25-2021, 01:53 PM
Last Post: CluelessChris
  Websocket conection closes abnormally Joshua_Omolo 2 3,911 Feb-17-2021, 08:03 AM
Last Post: Joshua_Omolo
  want to stream rtsp from my Pi camera korenron 0 2,360 Dec-28-2020, 08:23 AM
Last Post: korenron
  Websocket server not async? korenron 0 1,771 Sep-23-2019, 01:40 PM
Last Post: korenron
  define a variable before looped websocket korenron 0 1,859 Sep-22-2019, 12:53 PM
Last Post: korenron
  How to combine data taken from server between client and GUI? bescf 9 7,597 Apr-02-2019, 11:48 AM
Last Post: bescf
  Send data BMP180 between client and server trought module socket smalhao 0 2,844 Jul-30-2018, 12:56 PM
Last Post: smalhao
  need to understand better __setup__.py, websocket-client, packaging penright 0 2,967 Jul-08-2018, 09:40 PM
Last Post: penright

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020