How to disconnect Websocket connection?

How do I disconnect the websocket connection and stop getting market data feeds?

@akshay.l

To disconnect from a WebSocket connection and stop receiving market data feeds, you need to find and use the appropriate method provided by the WebSocket library you’re using. In JavaScript, if you’re using the ws library, you can call the close method on your WebSocket instance like this:

// Assuming ws is your WebSocket instance
ws.close();

This will terminate the WebSocket connection, ceasing the market data feeds. The method to close the connection may vary depending on the specific WebSocket library or implementation you’re using.

Thanks!!

Hello Shanmu,

Thank you for your response. I am using python and I could not find the appropriate method in the WebSocket library. Can you please help me with python? I tried:

dir(websockets)
[‘AbortHandshake’, ‘BasicAuthWebSocketServerProtocol’, ‘ConnectionClosed’, ‘ConnectionClosedError’, ‘ConnectionClosedOK’, ‘Data’, ‘DuplicateParameter’, ‘ExtensionHeader’, ‘ExtensionParameter’, ‘InvalidHandshake’, ‘InvalidHeader’, ‘InvalidHeaderFormat’, ‘InvalidHeaderValue’, ‘InvalidMessage’, ‘InvalidOrigin’, ‘InvalidParameterName’, ‘InvalidParameterValue’, ‘InvalidState’, ‘InvalidStatusCode’, ‘InvalidURI’, ‘InvalidUpgrade’, ‘NegotiationError’, ‘Origin’, ‘PayloadTooBig’, ‘ProtocolError’, ‘RedirectHandshake’, ‘SecurityError’, ‘Subprotocol’, ‘WebSocketClientProtocol’, ‘WebSocketCommonProtocol’, ‘WebSocketException’, ‘WebSocketProtocolError’, ‘WebSocketServer’, ‘WebSocketServerProtocol’, ‘WebSocketURI’, ‘all’, ‘builtins’, ‘cached’, ‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘path’, ‘spec’, ‘version’, ‘auth’, ‘basic_auth_protocol_factory’, ‘client’, ‘connect’, ‘exceptions’, ‘extensions’, ‘framing’, ‘handshake’, ‘headers’, ‘http’, ‘parse_uri’, ‘protocol’, ‘serve’, ‘server’, ‘speedups’, ‘typing’, ‘unix_connect’, ‘unix_serve’, ‘uri’, ‘version’]

Thank you in advance!

@akshay.l , Are you utilizing the Upstox Python SDK or another library to establish a WebSocket connection? The connection closing method might differ slightly depending on the library used.

Hello Pradeep,
Yes I am using Upstox Python SDK.

Assuming you are utilizing the Python websockets library as illustrated in this example here in websockets example with Upstox API, it is advisable to employ await websocket.close() for terminating the connection upon completion.

Go ahead and give it a shot, then let me know how it goes. Feel free to share your code snippets here for a more thorough evaluation.

Hey Pradeep, I tried using await websocket.close() and it worked. Thanks a lot! :slight_smile:
I just added a time counter to test like below:

 # Connect to the WebSocket with SSL context
   async with websockets.connect(response.data.authorized_redirect_uri, ssl=ssl_context) as websocket:
       timeout = time.time() + 60  # 1 minutes from now
       print('Connection established')
 ...
 ...
 ...
 ...
 
 # Continuously receive and decode data from WebSocket
    while True:
        message = await websocket.recv()
        decoded_data = decode_protobuf(message)

        # Convert the decoded data to a dictionary
        data_dict = MessageToDict(decoded_data)

        # Print the dictionary representation
        print(json.dumps(data_dict))

        if time.time() > timeout:
            await websocket.close()
            print('Connection stopped')

Got the response as:
Connection stopped
websockets.exceptions.ConnectionClosedOK: code = 1000 (OK), no reason

Great to know that it worked for you. Happy coding!