Monday, October 11, 2010

Push service from Google

Update: the first version of this post contained factual errors. Thanks to Tejas for pointing these out, you can find his experiences with C2DM here. The example program was also updated to fix these errors.

Android 2.2's most important improvement is without doubt the significantly faster virtual machine. The API, however, got some new features too. For me, the most intriguing new feature is the access to Google's experimental push services.

I have already posted about push feature in relation to iBus//Mobile asynchronous communication package so push is not new to Android. There are also open source alternatives like MQTT, Deacon or Urban Airship. It has also been present in certain Google applications from the beginning. Whenever Android Gmail application sends a notification when a new mail arrives on the server, you see push in operation. It is pretty fast, according to my experiences, even though time to time it takes longer time for the notification to arrive. 2.2 opened that already existing mechanism to general-purpose applications, even though the push service is still in early beta.

In order to do push, either one has proper push bearer (a network mechanism able to deliver unsolicited messages to the device) or such mechanism is simulated. Currently only two real push bearers are deployed widely on mobile networks, SMS and Blackberry's proprietary push solution. SMS is costly and the Blackberry solution is available only for Blackberry so the push bearer has to be simulated. The most common simulation method relies on the device to open a TCP connection to the push server. As the TCP stream is full-duplex, the server can send the push message to the device, provided that the stream is still alive. And that's the tricky bit whose complexity should not be underestimated. TCP streams time out if there is no communication on them and the other side does not necessarily notice it. A constant ping-pong traffic needs to be generated to prevent this. Frequency, however, is critical. Too frequent ping-pongs and the data cost associated to ping-ponging will be unacceptable and the battery is drawn down quickly. Too rare ping-pongs and the device or the server will not notice that the TCP stream was closed, only after a long timeout. For the user, it means that he did not get his urgent message immediately, only after, say, an hour.

There is no perfect solution to this problem, therefore it is a good feeling that Google made its best to create a simulated push bearer, along with the server that is able to keep that many TCP sockets open and shared that infrastructure with us. The Google push architecture has the following elements.

  • The device that authenticates and registers with the push server (Cloud to Device Messaging in Google lingo) and provides authentication ticket to the 3rd Party Application Server.
  • 3rd Party Application Server that uses the authentication ticket generated by the device to send push requests to the push server.
  • Push server provided by Google that authenticates push requests from 3rd Party Application Servers and delivers the messages to the devices.
Click here to download the example program.

Cloud to Device Messaging (C2DM) has many tricky aspects but I wanted to create a test program that is as simple as possible. To try it out, you need the following:

  • A PC connected to the Internet so that Google C2DM servers can be reached (and C2DM servers can reach us).
  • Android emulator with 2.2+Google API AVD created (API level 8).
  • Google App Engine SDK for Python.
  • A Google account that you register with Google for push. It is better not to use your real e-mail address because you have to insert the password for this account into the server script. Use aexp.push as package name for the application in the registration form.
Do the following.
  • Unpack the download package. Start the Android emulator. Create a Google account if no such account exists (Settings/Accounts & sync). This account does not have to be the same that you registered for push, it can be any Google account that you know the password for.
  • Enter the "client" directory in the download package, update client/src/aexp/push/Config.java with the Google account you registered for push (C2DM_SENDER)
  • Open server/pushserver/pushserver.py and update it with your push Google account (lines 165 and 167). Unfortunately here you have to insert the password for your push Google account into the server script.
  • Enter the "server" directory, customize "ae_pushserver.sh" according to your directory layout and start it. Google App Engine SDK starts.
  • Start the Push application on the emulator, select the account using the Menu and wait for the "Registered" message to appear. Now the server application is ready to deliver push messages.
  • Go to http://localhost:8080 with your browser and type a message. Select the account from the drop-down list that you configured on the device and click the submit button. The message should appear on the emulator screen.


If you want to do the same trick from a real phone, you need a server with fix IP address or a fix DNS name and run the App Engine SDK there. Or better, deploy it on the real thing, on Google infrastructure. In any case, make sure that you update the server address in client/src/aexp/push/Config.java (and delete client/bin/classes directory as there are occassionally troubles with javac's dependency resolution).

About the application. The application reuses c2dm utility library from the JumpNote sample application. When the account is selected, the application registers with the push server using the phone user's credentials and the application ID that you registered with Google. Then comes the interesting bit. After the application registers with the push server, it sends the registration ID to the application server. The application server will use the registration ID to talk to the push server when it wants to do push. The server uses the Google account that you selected for push for authentication and therefore it needs the password for this account.

The implementation in the example application server program is a not efficient as it always requests an authentication token before it sends the push message. In an efficient implementation the token can be requested only once and can be used to authenticate many messages. Eventually the token expires and only then should new authentication token requested.

The client application also sends the client account name to the server but it is not used for authentication, it is only needed so that you can select the push target by account name in the web application's drop-down menu.

62 comments:

Anonymous said...

how can i run this on windows? what are the process to run the app engine?

Unknown said...

I forgot about the push notifications! Thanks for reminding us all to check this out again and providing a code snippet! Maybe you'd like to start an account on github with all your valuable code snippets. I'm interested in how Google's technical implementation will differ from Urban Airship's: http://github.com/urbanairship/android-push-sample

Gabor Paller said...

Anonymous, use the link in the post to download the Windows version of the Google App Engine SDK. Then tweak server/ae_pushserver.sh. Currently it is a Unix shell script but is only one line long. Turning it into a .bat file should be easy. I have never tried the App Engine SDK on Windows but it should work then without any further changes.

Gabor Paller said...

Hi, Kevin, nice to see you. :-)

I added Urban Airship's link to the post, when I mention alternatives. Doing a comparison of C2DM to these would be an interesting project, maybe I do it once I get out of this pretty busy period. One thing to note: the server part of the solution that uses TCP stream to simulate push may look easy but the scalability problems should not be underestimated. Very quickly you can end up with a server that needs to handle 10s of thousands of 100s of thousands TCP connections - mostly idle connections though.

As far as I know, direct UDP to devices is blocked by operators at various levels.

Petar K. said...

Thanks the tutorial it is very helpful. I cannot however get the server side code to work. When i submit the message i get the following error :
Message sent, status:
Unauthorized
Error 401
I think im not configuring the pushserver.py you posted properly. The emulator seems to register fine as i see no error in the DDMS when i register.
Any Help would be great. Thanks

Gabor Paller said...

Hi, Petar,

Do you try to run the server part in the SDK as proposed by the post or have you deployed it on Google's servers?

Gabor Paller said...

OK, one more. Do you know exactly which part is unauthorized? The Android client may receive this error message when it talks to the Google App Engine SDK. Or the server part may be unauthorized when it tries to connect to Google's push server.

Have you configured a valid Google account in your emulator?

Petar K. said...

I tried running the server part through the SDK as proposed. The previous error seems to be gone and a new error appears, this one is a bit more descriptive when i select "Send Message" I get the following;
Message Sent, status: Error=NotRegistered
but I believe the simulator is registered as my simulator email is appearing in the dropdown menu.
I am going to try running it on Google's server and see what happens.

Gabor Paller said...

This is strange, I have just tried myself and it worked fine for me.

Can you see the "onRegistered() done" message in the Android log? (adb logcat). There is a chance that the server side stored the registration with an earlier, successful registration attempt (this simplistic server never deletes a registration).

Unknown said...

Hey Gabor...

Very nice tutorial :)
However, even I'm getting the same error:

Message sent, status:
Unauthorized
Error 401

The registration goes well on the device. I see the onRegistered called in the "adb logcat" stream.

I'm not sure what part on the server is not authorized ? I'll keep looking for, if found will update.

Petar K. said...

yup I get the "onRegistered() done" in the log ill keep trying if I get it working I will post.

Unknown said...

btw, I have it running on windows.
So, my batch file (instead of your shell script is )

python C:\devtools\google_app_engine\dev_appserver.py --port=8080 C:\try\push\server\pushserver --address=0.0.0.0

Also, I changed IP addr in the Config.java to point to my machine.

Unknown said...

I put a Log in the sendToken method of the NetworkCommunication.java.

I see that the authToken is null. Do you think this could be an issue ?

Gabor Paller said...

"Also, I changed IP addr in the Config.java to point to my machine. "

That's not necessary. That particular IP address in Config.java is the localhost of the machine running the emulator. It is an Android emulator convention. The reason is that localhost or 127.0.0.1 on the emulator refers to the Linux running the Android framework, the phone itself. In order to get to the real localhost (the PC running the emulator), they came up with that special address.

I have never tried this trick in Windows but it should work there too.

Gabor Paller said...

"I see that the authToken is null. "

Have you configured a valid Google account in the emulator's Settings/Accounts menu?

Unknown said...

I got the IP thing. But changing the IP is necessary if running on the device (which i did). Right ?

And I have a proper google account set up on the emulator. And as I said I ran it on my device, which has my google account set correctly, still I get a null token for some reason !

Today I was planning to try and explicitly use the setPassword method of the AccountManager to see if it works.. Let me know if you have any suggestions.

Unknown said...

Update:
Apparently, I found out that the authToken was getting expired. I used the following call just before getting the token:
accountManager.invalidateAuthToken("com.google", null);

Now, I'm able to get the token. However, the server is still not happy with me. It still says Not Autorized 401 !

btw, Let me know if I'm commenting too much on your post.. I'll stop bugging you if you don't want :D

Gabor Paller said...

OK, I managed to reproduce it. When you start the Android client application and select the account, have you noticed the small black triangle in the upper left corner of the phone screen, in the notification tray? This warns you that you have to grant first right for this application to access ac2dm token. Also, for good measure, I added --clear_datastore to the script starting the server but I don't think it was the cause.

Unknown said...

Hi Gabor,

I got it all working !! :) Phew...
Well I found out that you really don't need to send anything else than the registration id from the device to the server.

The server should use the authToken not from the device ID but the one which is acquired by using the ROLE Account (your gmail id in your app case). This authToken you can acquire by any means (say curl) by using your password.

So I got into their whitelist using my email id and then got the authToken using the curl command. Used this while positing the message to the server.

I got some information here

I would again like to thank you for writing this tutorial which simplified a lot of things.

~Tej

Gabor Paller said...

Tejas, thanks for all the effort. The post and the example program did contain factual errors. I fixed these and mentioned your name in the update note. :-)

Unknown said...

Thanks a lot Gabor.
I have also put together an article in my recently started blog.

You can find it here:
http://techtej.blogspot.com/2010/10/android-c2dm-messaging-server-push-for.html

I have a link back to your blog as well as your name on it.
Thanks again :)

Gabor Paller said...

I linked to your post. Thanks again.

Anonymous said...

I am getting below error any pointer would be highly appreciated.. one more thing from push example during registration you are always sending sender is as gaborpaller@gmail.com.


11-23 16:03:29.058: WARN/ActivityManager(63): Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gsf (has extras) }: not found

Anonymous said...

I am getting below error any pointer would be highly appreciated..

11-23 16:03:29.058: WARN/ActivityManager(63): Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gsf (has extras) }: not found

Gabor Paller said...

Anonymous, is your device 2.2 and is Android Market available on the device? If it is an emulator, have you created the virtual device as 2.2+Google APIs?

Anonymous said...

Traceback (most recent call last):
File "/home/Downloads/server/google_appengine/google/appengine/ext/webapp/__init__.py", line 517, in __call__
handler.post(*groups)
File "/home/Downloads/server/pushserver/pushserver.py", line 65, in post
status = self.sendMessage( accountName, registrationId, text )
File "/home/Downloads/server/pushserver/pushserver.py", line 90, in sendMessage
'Authorization': 'GoogleLogin auth='+authToken
File "/home/Downloads/server/google_appengine/google/appengine/api/urlfetch.py", line 245, in fetch
return rpc.get_result()
File "/home/Downloads/server/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 534, in get_result
return self.__get_result_hook(self)
File "/home/Downloads/server/google_appengine/google/appengine/api/urlfetch.py", line 341, in _get_fetch_result
raise SSLCertificateError(str(err))
SSLCertificateError: ApplicationError: 6 Host android.apis.google.com returned an invalid certificate (hostname mismatch): {'notAfter': 'Jan 5 22:55:09 2012 GMT', 'subjectAltName': (('DNS', '*.google.com'), ('DNS', 'google.com'), ('DNS', '*.atggl.com'), ('DNS', '*.youtube.com'), ('DNS', '*.ytimg.com'), ('DNS', '*.google.com.br'), ('DNS', '*.google.co.in'), ('DNS', '*.google.es'), ('DNS', '*.google.co.uk'), ('DNS', '*.google.ca'), ('DNS', '*.google.fr'), ('DNS', '*.google.pt'), ('DNS', '*.google.it'), ('DNS', '*.google.de'), ('DNS', '*.google.cl'), ('DNS', '*.google.pl'), ('DNS', '*.google.nl'), ('DNS', '*.google.com.au'), ('DNS', '*.google.co.jp'), ('DNS', '*.google.hu'), ('DNS', '*.google.com.mx'), ('DNS', '*.google.com.ar'), ('DNS', '*.google.com.co'), ('DNS', '*.google.com.vn'), ('DNS', '*.google.com.tr')), 'subject': ((('countryName', u'US'),), (('stateOrProvinceName', u'California'),), (('localityName', u'Mountain View'),), (('organizationName', u'Google Inc'),), (('commonName', u'*.google.com'),))}
To learn more, see http://code.google.com/appengine/kb/general.html#rpcssl



I get this when I push a message. Did anyone get this error?? I checked out the link http://code.google.com/appengine/kb/general.html#rpcssl but it gives me a patch to python for the ssl package but I have python 2.6 and the patch doesnt work on it.

Unknown said...

Thanks for your post. It's very helpful. However, when I try your example, I got below error. Is there anything wrong? Thanks.

Traceback (most recent call last):
File "/home/xinyu/Downloads/google_appengine/google/appengine/ext/webapp/__init__.py", line 517, in __call__
handler.post(*groups)
File "/home/xinyu/Downloads/push/server/pushserver/pushserver.py", line 65, in post
status = self.sendMessage( accountName, registrationId, text )
File "/home/xinyu/Downloads/push/server/pushserver/pushserver.py", line 90, in sendMessage
'Authorization': 'GoogleLogin auth='+authToken
File "/home/xinyu/Downloads/google_appengine/google/appengine/api/urlfetch.py", line 245, in fetch
return rpc.get_result()
File "/home/xinyu/Downloads/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 534, in get_result
return self.__get_result_hook(self)
File "/home/xinyu/Downloads/google_appengine/google/appengine/api/urlfetch.py", line 341, in _get_fetch_result
raise SSLCertificateError(str(err))
SSLCertificateError: ApplicationError: 6 Host android.apis.google.com returned an invalid certificate (hostname mismatch): {'notAfter': 'Jan 5 22:55:09 2012 GMT', 'subjectAltName': (('DNS', '*.google.com'), ('DNS', 'google.com'), ('DNS', '*.atggl.com'), ('DNS', '*.youtube.com'), ('DNS', '*.ytimg.com'), ('DNS', '*.google.com.br'), ('DNS', '*.google.co.in'), ('DNS', '*.google.es'), ('DNS', '*.google.co.uk'), ('DNS', '*.google.ca'), ('DNS', '*.google.fr'), ('DNS', '*.google.pt'), ('DNS', '*.google.it'), ('DNS', '*.google.de'), ('DNS', '*.google.cl'), ('DNS', '*.google.pl'), ('DNS', '*.google.nl'), ('DNS', '*.google.com.au'), ('DNS', '*.google.co.jp'), ('DNS', '*.google.hu'), ('DNS', '*.google.com.mx'), ('DNS', '*.google.com.ar'), ('DNS', '*.google.com.co'), ('DNS', '*.google.com.vn'), ('DNS', '*.google.com.tr')), 'subject': ((('countryName', u'US'),), (('stateOrProvinceName', u'California'),), (('localityName', u'Mountain View'),), (('organizationName', u'Google Inc'),), (('commonName', u'*.google.com'),))}
To learn more, see http://code.google.com/appengine/kb/general.html#rpcssl

Gabor Paller said...

yu, did you install the ssl module as the link at the end of the exception proposes?

Unknown said...

The version of my python is 2.6.5. So the SSL module does not need to install. Currently I have to change https://... to http to avoid exception. But there should be a better solution.

Gabor Paller said...

Do you also have this problem if you deploy on the real Google App Engine or only with the SDK?

Unknown said...

Not yet. The error appears on my local machine.

Another question is, is there any document about the length of registration ID? Thanks.

Unknown said...

I'm getting the same cert error reported by yu. I have python 2.7.1 installed. I'm also running the SDK on my local machine.

On startup of the dev_appserver, I see some warnings, probably not relevant:

WARNING 2011-03-01 20:14:50,437 datastore_file_stub.py:573] Could not read data
store data from c:\users\mpeters\appdata\local\temp\dev_appserver.datastore
WARNING 2011-03-01 20:14:50,453 dev_appserver.py:3700] Could not initialize ima
ges API; you are likely missing the Python "PIL" module. ImportError: No module
named _imaging

Does anybody know how to fix this?

Mark

Unknown said...

Ack. Never mind - after some research, I see now what yu meant about changing https to http - that has to be changed in the pushserver.py script to work around the issue.

Once I did that, that app worked.

Thanks for an excellent tutorial, Gabor!

Mark

Anonymous said...

Hi Mark and Yu,
I changed https to http, the exception is gone, however, I got "Message sent, status: Unauthorized, Error 401" message.
Can you explain how did you solve the issue?
Thanks.
-Steve

Anonymous said...

Never mind. It is working for http mode.
-Steve

Unknown said...

Hi Mr. Gabor, thanks for your article. I try to run the server but I get this error log on. Please help, thanks

2011-03-16 00:14:23 Running command: "['C:\\Engine\\Python32\\python.exe', 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py', '--admin_console_server=', '--port=8080', 'C:\\Subversion\\push\\server\\pushserver']"

Traceback (most recent call last):

File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 71, in

run_file(__file__, globals())

File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 67, in run_file

execfile(script_path, globals_)

NameError: global name 'execfile' is not defined

2011-03-16 00:14:23 (Process exited with code 1)

The Great Mind said...
This comment has been removed by the author.
The Great Mind said...

Apologies. Got it working. Was using an unregistered google account id.

Super..

andronidian said...

thnx for the post. I tried to run the server side, but i got this error :
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 702, in __call__
handler.post(*groups)
File "C:\Documents and Settings\Administrateur\Mes documents\Downloads\Compressed\push\server\pushserver\pushserver.py", line 65, in post
status = self.sendMessage( accountName, registrationId, text )
File "C:\Documents and Settings\Administrateur\Mes documents\Downloads\Compressed\push\server\pushserver\pushserver.py", line 90, in sendMessage
'Authorization': 'GoogleLogin auth='+authToken
File "C:\Program Files\Google\google_appengine\google\appengine\api\urlfetch.py", line 260, in fetch
return rpc.get_result()
File "C:\Program Files\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 592, in get_result
return self.__get_result_hook(self)
File "C:\Program Files\Google\google_appengine\google\appengine\api\urlfetch.py", line 367, in _get_fetch_result
raise SSLCertificateError(str(err))
SSLCertificateError: ApplicationError: 6 Host android.apis.google.com returned an invalid certificate (hostname mismatch): {'notAfter': 'Jun 7 04:50:43 2012 GMT', 'subjectAltName': (('DNS', '*.google.com'), ('DNS', 'google.com'), ('DNS', '*.atggl.com'), ('DNS', '*.youtube.com'), ('DNS', 'youtube.com'), ('DNS', '*.ytimg.com'), ('DNS', '*.google.com.br'), ('DNS', '*.google.co.in'), ('DNS', '*.google.es'), ('DNS', '*.google.co.uk'), ('DNS', '*.google.ca'), ('DNS', '*.google.fr'), ('DNS', '*.google.pt'), ('DNS', '*.google.it'), ('DNS', '*.google.de'), ('DNS', '*.google.cl'), ('DNS', '*.google.pl'), ('DNS', '*.google.nl'), ('DNS', '*.google.com.au'), ('DNS', '*.google.co.jp'), ('DNS', '*.google.hu'), ('DNS', '*.google.com.mx'), ('DNS', '*.google.com.ar'), ('DNS', '*.google.com.co'), ('DNS', '*.google.com.vn'), ('DNS', '*.google.com.tr'), ('DNS', '*.android.com'), ('DNS', '*.googlecommerce.com')), 'subject': ((('countryName', u'US'),), (('stateOrProvinceName', u'California'),), (('localityName', u'Mountain View'),), (('organizationName', u'Google Inc'),), (('commonName', u'*.google.com'),))}
To learn more, see http://code.google.com/appengine/kb/general.html#rpcssl

So, any one knows what should i do ?

Prakash-The rider said...

I m able to deploy it and work properly, can u please enlighten me how u wrote server side python script?
i want to send automated messages..to app in my andorid

jakrapong.pandez said...

hi
i have problem on server side
i change email and pass word at line 165,167
then change account name at line 28


when i send message it show No registration for 'mymail@gmail.com'

jakrapong.pandez said...
This comment has been removed by the author.
jakrapong.pandez said...

hi
i have problem on server side
i change email and pass word at line 165,167
then change account name at line 28 to my sender mail

when i send message it show
" No registration for 'mymail@gmail.com' "

Gabor Paller said...

pandez, have you registered your account with C2DM? Link in the post.

jakrapong.pandez said...

Gabor Paller ,thx for reply

i have registered my account with c2dm .
i receive google invited mail already.

but i confuse about change ae_pushserver.sh
plz suggest me

jakrapong.pandez said...

how i set path in ae_pushserver.sh if my folder is in window
c:/.../../?
im a beginner .

jakrapong.pandez said...

C:\Program Files\Google\google_appengine\dev_appserver.py --port=8080 C:\Documents and Settings\kingkan_ban\My Documents\opensource\push\server\pushserver

what's wrong

andreasv said...

Hi,

Even without changing the username and passwd I cannot see the default email as shown in picture above

Therefore when i send message I get

No registration for '-'

Ho can I fix this?

Martin Cuddy said...

will C2DM only work with API 8 in the Emulator

Gabor Paller said...

Martin, you need to have the Google extensions installed (Google APIs).

gaurav said...

When I try to register my emulator by going in settings/Accounts & Sync/Add account, I get following alert after adding my details.

"Set up could not finish.

Unable to open connection to server".

Note that I've active internet connection.

Any help appreciated.

Thanks in advance.

gaurav said...

@Tejas,
I'm running it on windows.

What are the changes that need to be done from the source code provided here?

I'm new to android development, so any help appreciated.

Thanks in advance.

sugan said...

Anyone can help me to configure the ae_pushserver.sh.because i have configured after compilation it shows "Force to close " in an emulator

sugan said...
This comment has been removed by the author.
sugan said...
This comment has been removed by the author.
sugan said...

Gaurav for account creation u have to Install Android market in your emulator follow this link (http://www.tech-recipes.com/rx/10004/accessing-android-market-from-android-sdk/) and install Android Market k

Himansu said...

When I open http://localhost:8080 in web browser i can't see my registered username on the list. I have already registered my account to google c2dm. Also when i try to send some message i get an error as "No registration for '-'" . How do I resolve this error?
Thank You.

Unknown said...

Excelent post.
Can anyone post some help for the client side. How do i import the package into eclipse? Is there any other way to get the .apk file?

Thanks.

Gabor Paller said...

Unknown, personally I would create a new Eclipse project and would copy code/resources into the new Eclipse project from the Android project that can be downloaded from here.

But here are some more sophisticated advices.

Anonymous said...

Thank you Gabor, it worked!!.
Im just having some issues with the registration process,
i´ve changed config.java in client, pushserver.py, ae_pushserver.sh (wich has changed to bat) in server side.
Started the push app in the emulator and selected the account but i dont get the "Register" message and also it doesn´t apear in the drop down list in my browser.

Thanks for your time.

Anonymous said...

Finally i got the register message.
After that i had the error mentioned by "yu" and after changing https with http on pushserver.py it worked.

Thank you all for the help, now the next step is to know exactly how the code works.

rydermartin said...

The most common simulation method relies on the device to open a TCP connection to the push server. As the TCP stream is full-duplex, the server can send the push message to the device, provided that the stream is still alive