'Sending multiple json requests in websocket API using python by making one time connection

I want to create a WebSocket connection once and send multiple JSON requests under the same connection. In my code, I have a separate placeholder to make the connection and pass JSON requests(send from different functions). But, each time I call the function, it creates a connection every time and sends JSON requests. So here, if I use the function wss_connection and pass parameter json_request anytime, it will connect and disconnect again and again. Is there a way to make the connection only once and send json data to Websocket API ? Please advise. Thanks

def wss_connection(self, json_request):
        ws = create_connection("url" + auth_token + "&clientId=" + client_id + "")
        ws.send(json.dumps(json_request))
        result = ws.recv()
        json_response = 'Result: {}'.format(result)
        # ws.close()
        return json_response


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source