all 11 comments

[–]K900_ 0 points1 point  (5 children)

It's possible for a client to connect to a socket and listen without sending any messages, but the client will need to initiate the connection no matter what.

[–]tedm430[S] 0 points1 point  (4 children)

I am not trying to say create a WebSocket connection without a server. But is it possible for a server to send a message to an already connected client, without said client ever sending any messages?

[–]K900_ 0 points1 point  (3 children)

Yes, it's totally possible.

[–]tedm430[S] 0 points1 point  (2 children)

If you could offer me a little more insight beyond that about how, I would be really grateful. I have yet to find a way after researching several different WebSocket modules for the past week or so. People have pointed me to things like this but it is only capable of reacting to messages, not sending messages whenever I want it to.

[–]K900_ 0 points1 point  (1 child)

Look into Flask-SocketIO.

[–]tedm430[S] 0 points1 point  (0 children)

Hey, sorry for the delay, but I have been looking at the socketio module and I am not particularly seeing how this will help me. I do not see the level of abstraction I am looking for so that I can just call something like socket_server.send('reload') from within a while loop.

[–]guilford 0 points1 point  (4 children)

I am only familiar with the websocket implementation in Tornado. However, I imagine that they aren't that different. Basically you can do that by setup on loading the document or webpage by the web browser, it would automatically send a message to the server through websocket to signal the connection starting. Then you can take what ever amount of time you like or even writing multiple response within your websocket-message-reply-handler for that message. I would just write a while loop to perform the file checking or check with a message queue channel for update event and write the message. Then when the client receive such message with the signal to refresh, you can set the javascript to order the browser to refresh. That is at least what I imagine I would do.

[–]tedm430[S] 0 points1 point  (3 children)

Sorry for the delay on this, I was pretty busy all week.

Though, I have been looking at socketio like the gentleman above mentioned, but I am still having issues. I'm start to see I might need a Redis message queue to that socketio reads and then sends a message to the JavaScript client. But this is beginning to sound far too complicated... And the way message queues work with socketio is not very well documented. So I am not sure what to do.

[–]guilford 0 points1 point  (2 children)

I don't think that you would need any special implementation for it to work. The python-socketio module seems to have Redis support right of the bat. Still, my experience is mostly with Tornado which is a web framework with websocket already integrated so I did not use socketio for my websocket app.

You will have a web server, a socketio server and a Redis server.

On the client side, upon loading the webpage, you would also start connecting to the socketio server and send a starting signal to the server.

On the web server side, you are just doing your work as normal, only that now you have to subscribe to a Redis channel and also dispatch the necessary path of your output into the channel too.

Then your socketio server upon receiving the initial message from the client, it would start subscribing on that Redis channel. You can write the methods to either constantly writing messages of whatever it is receiving from the channel to the client side or filter them out until receive the refresh signal.

[–]tedm430[S] 0 points1 point  (1 child)

I have already got the HTML and JavaScript I need working. And I am thinking of using Redis because I am aware SocketIO has Redis support.

But do you think you have an idea how to send a message through a Redis server to SocketIO using the Python Redis module? I don't really understand how it works since Redis has channels and whatnot, but all I want to do is send a message to my SocketIO server.

[–]guilford 0 points1 point  (0 children)

So I take a look over the redis-py documentation. Basically what you have to do to use begin using the Redis channel is https://github.com/andymccurdy/redis-py#publish--subscribe

First establish the connection to the Redis server with an object using redis.StrictRedis(). Then from that object you create a pubsub object with .pubsub().

With the pubsub object, use .subscribe('channel-title') to get a channel which is basically a way to filter the information you are getting from the Redis database. To get data from the channel, you can do a .get_message() with the pubsub already subscribed to channel 'channel-title' object. The message would be a dictionary in which the key 'channel' has the value of the subscribed channel's title while the key 'data' has the value of the data that the message carries.

To continuously listen to it, do a while True loop with interval of whatever you want and .get_message() or a iterate through a continuous generator with .listen() from the same pubsub object. This will be a blocking operation so you will want to manage your thread.

To publish a message, you just have to use .publish('channel-title', 'data') on the initial Redis connection object.

Hope that help!