Automatically reconnect a Python Tornado Websocket Client Connection - python

I am using Python Tornado's websocket_connect function ( http://tornadokevinlee.readthedocs.org/en/latest/websocket.html ) to listen to a websocket feed.
Sometimes the websocket server will die, or disconnect the client (this is detectable ) and I need to figure out a way to clean up the old connection and reconnect the client. I could of course just restart the program but what is the proper way to do this through tornado?

There's nothing automatic for this. When it disconnects, just call websocket_connect() again.

Related

Python Socket reconnect after connection failure [duplicate]

Okay, I've read this post in search for the right answer, but it does not seem to serve my purpose.
This Question
Now, getting to the trouble:
I have a conventional client-server architecture in C (all sockets are non-blocking), where the server is listening for incoming connections and the client tries to connect. The first connect succeeds and everything goes on just fine until I press Ctrl + C on my server.
The client side of the code detects that the connection is lost and arms a retry timer.
The client code is supposed to try a reconnect on the server again and again by using the POSIX interval timers on each timer popping. It however, does not close the socket or start out afresh. Now, every time it retries the connection, the connect() returns
Transport endpoint is already connected
Even after restarting the server, which uses the SO_REUSEADDR and successfully starts, the connect does not complete.
One thing that I will need to implement is the signal handler on the server for the shutdown on Ctrl+C.
But still, do I need to close the socket descriptor on the client side and start afresh every time a disconnect happens, or is there a way out of this?
sockets cannot be reused.
Once the connection a socket served has gone down in both directions, the socket is unusable.
close() the client socket on loss of connection and create a new socket for a new connection.
Update (based on the comments below):
In the OP's case one side (the server side) went down (by means of the server process ending). This implies all sockets held by this process are implicitly close()ed and therefore shutdown() in both directions.

Server Sent Events detect client disconnect?

I have a lighttpd server with python installed, its using cgi. I'm able to set up a connection with Server Sent Events, but I'm unsure of how to detect if the client disconnects. I read somewhere that its impossible to tell whether a client disconnects or not, unless you send a message to detect it. I'm unsure of how to detect the client disconnect after sending a message. Whenever I send a message I just do...
print(message)
sys.stdout.flush()
Do I have to read stdin to check if the client disconnected or not?
The next version of lighttpd (1.4.40) detects if a client disconnects and sends a TERM signal to the CGI if the CGI is still running.

Receive data on server side with django-websocket-redis?

I'm working with django-websocket-redis lib, that allow establish websockets over uwsgi in separated django loop.
By the documentation I understand well how to send data from server through websockets, but I don't understand how to receive.
Basically I have client and I want to send periodically from the client to server status. I don't understand what I need to do, to handle receiving messages from client on server side? What URL I should use on client?
You can achieve that by using periodically ajax calls from client to server. From documentation:
A client wishing to trigger events on the server side, shall use
XMLHttpRequests (Ajax), as they are much more suitable, rather than
messages sent via Websockets. The main purpose for Websockets is to
communicate asynchronously from the server to the client.
Unfortunately I was unable to find the way to achieve it using just websocket messages.

How server alone can push data to client browser using python and websockets

I'm trying to push some messages to client (browser) without any request.
For that I use WebSockets and python to do. I know WebSockets provide a full-duplex communication, but I just need server to client push. I could only find bi-directional communication examples for WebSockets over internet, for a request a response model. When I tried to send in an infinite loop from server after handshake process, the browser hung up.
the code I used is in this post
Is there any solution to do that or whether it is better to go for SSE..
Once the client initiates the web socket connection the server then is eligible to send anything to client side

Send data to websockethandler on closing connection

I have Tornado websocket handler and I am sending messages from my browser ( I have override on_message,on_close,open).
In javascript on close I want to send some data to handler ( to clean some storages, I am sending some numbers in json like {'storage':22, 'time':96} ).
How in websocket handler in tornado to receive that closing message ?
I looked at close and on_close but there is no option to receive data.
If I understand what you're asking for correctly, it's impossible.
You want to make sure that when the connection is closed, and the browser calls the on_close function on your client-side JavaScript code, it can send some final data to the Tornado server.
But when the connection is closed, there's no way to send any more data. That's what it means to be closed.
What you need to do is create a "quit" or similar message, at the application level. When Tornado sends a "quit" message to the JS code, then it can send its final message; when Tornado receives that message, it can close the socket. (Of course this means you need to write your code to handle the case where that "graceful shutdown" never happens because, e.g., the client machine has been vaporized by a nuclear bomb.)

Categories

Resources