This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Selim's profile picture

Push Notifications with HTTP API

Started by Selim, 06 October 2015 - 08:01 PM
Selim #1
Posted 06 October 2015 - 10:01 PM
So, from my understanding of how push notifications work on other platforms is that the connection with the server is never closed, and that allows for much faster and easier notification handling so that a device does not have to check for data at an interval.

From what I understand of the ComputerCraft HTTP API it is not possible to keep a connection open for purposes like this, correct?

All I am asking is if it is realistically possible to have push style notifications in ComputerCraft.
TYKUHN2 #2
Posted 06 October 2015 - 10:09 PM
I don't think so no, but you can try HTTP headers such as a keep-alive. I am not sure how LuaJ and Java handle it so experiment!
Bomb Bloke #3
Posted 07 October 2015 - 05:38 AM
Indeed, you can't handle things in that manner. ComputerCraft scripts must request data when they want it. If they want notifications "as they happen", then they have to keep asking for them.
MKlegoman357 #4
Posted 07 October 2015 - 02:12 PM
I know somebody actually used a pretty clever solution for this. When you send a request to a server the server will not respond until a notification arrives (or the connection timeouts, which was 30 seconds). So if you are able to make the server hold the request for some time without sending a response you could do those instant notifications.

EDIT: here's the original idea: Mote.
Edited on 07 October 2015 - 12:20 PM
Selim #5
Posted 07 October 2015 - 04:40 PM
Ok, I will take this information into consideration.
Selim #6
Posted 07 October 2015 - 05:48 PM
Ok, I found my solution. All I am doing for those who are interested, I am just calling sleep(1) on the server end in PHP until it finds a file with the notification data in it.
Edited on 07 October 2015 - 03:48 PM
Wojbie #7
Posted 07 October 2015 - 06:24 PM
Ok, I found my solution. All I am doing for those who are interested, I am just calling sleep(1) on the server end in PHP until it finds a file with the notification data in it.
So if there is no file it will never return and loop forever? Or did you include some kind of sanity test in there?
Selim #8
Posted 07 October 2015 - 07:02 PM
Ok, I found my solution. All I am doing for those who are interested, I am just calling sleep(1) on the server end in PHP until it finds a file with the notification data in it.
So if there is no file it will never return and loop forever? Or did you include some kind of sanity test in there?
I believe ComputerCraft connections time out after 30 seconds.
Wojbie #9
Posted 07 October 2015 - 07:19 PM
I believe ComputerCraft connections time out after 30 seconds.
I meant on php side?
Selim #10
Posted 07 October 2015 - 07:22 PM
I believe ComputerCraft connections time out after 30 seconds.
I meant on php side?
I will be adding a counter for it to only check so many times first, then have close the connection. If the computer ends the connection first, the PHP script should stop.