Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket close method feature #782

Open
t1waz opened this issue Apr 4, 2024 · 1 comment
Open

Websocket close method feature #782

t1waz opened this issue Apr 4, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@t1waz
Copy link

t1waz commented Apr 4, 2024

Description

Currently, Robyn framework lacks the capability to programmatically close a WebSocket connection from within the handler method. This feature is critical for implementing server-side logic that demands the closure of a WebSocket connection under specific conditions, such as the expiration of a user's authorization period.

Almost all major frameworks got this functionality

Use Case

A practical example of this feature would be in scenarios where a user's temporary access for WebSocket communication expires. For instance, if a user is only authorized to maintain a WebSocket connection for a certain period, the server should have the ability to terminate the connection from within the WebSocket view/handler once this period elapses. This is crucial for maintaining security and efficient resource management.

Suggested Implementation

The implementation could involve enhancing the existing WebSocket handler class/methods, allowing them to invoke a close connection method. This method should be able to send a closing handshake message to the client and terminate the connection gracefully.

Example pseudo-code:

@websocket.on("message")
  def message(ws, msg) :
      if not is_valid(msg):
         ws.close()  <----- implement this 
@t1waz t1waz added the enhancement New feature or request label Apr 4, 2024
@aniketkumar7
Copy link

As you suggested earlier, Firstly, the WebSocket handler class would need to be enhanced to include a close method. This method would be responsible for sending a closing handshake message to the client and terminating the connection.

Here's a simplified example:

class WebSocketHandler:
def init(self, ws):
self.ws = ws

def close(self, code=1000, reason=""):
    # Send a closing handshake message to the client
    self.ws.send_close(code, reason)
    # Close the WebSocket connection
    self.ws.close()

Then, in WebSocket view, we could use this close method to terminate the connection under specific conditions:
As you suggested
Example pseudo-code:

@websocket.on("message")
def message(ws, msg) :
if not is_valid(msg):
ws.close() <----- implement this

Can you please provide reference to the code to contribute to it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants