request for adding unsubscribe function
forgive me if I miss it. I cannot find an unsubscribe function after I subscribed a security using subscribe().
True, but do you really need it? E.g. if you were to run a batch job with a subscribe() call, once it ends it just ends. Does the API mandate / n suggest / recommend / mention an unsubscribe action?
it doesnt mandate an unsubscribe action. But what I am planning is sort of combining it with Shiny. There will be multiple requests through out the day to subscribe the datafeed. If I do not kill those no longer used subscription, I am worried it will be a performance hog.
Maybe, maybe not. That is a conjecture at this point.
We do have shiny apps with long-running db connections. I typically open these at the start and just hold on to them. No leaks, no issues.
The API documentation mentions unsubscribe but the language used makes it seem like an optional actions. Page 53:
- Cancellation: Subscriptions (each subscription identified by its correlation ID) can be cancelled by the unsubscribe method of Session
That is can and not should or must.
Also, in src/subscribe.cpp we do not export the session object making it kinda hard to unsubscribe from it. But by existing the scope we get C++ to automatically dispose of it.
Anyway, pull requests welcome as always if you still think this is needed.
The current API design makes me wonder. In version 0.3.8, essentially
subscribe(securities=c("TYZ5 Comdty","/cusip/912810RE0@BGN"),
fields=c("BID"),
fun=function(x) print(str(x$topic)))
blocks the entire process and waits for any messages. Once they arrive, they will be processed by fun and the waiting continues. The resulting user experience is (IMHO) horrible:
In RStudio, you run the script using subscribe(). After some time, you stop the script by clicking on the red stop icon. After a few seconds, RStudio notifies you that the script crashed and needs to restart the session completely. RStudio restarts, all variables are lost and any changes to the file you made after starting the script.
I can think of the following solutions:
- I am not sure what is triggered by the red stop topic. A graceful method to stop listing for subscriptions seems to be triggering
Rcpp::internal::InterruptedException(handled by subscribe.cpp line 230), but apparently the red stop icon does not triggerRcpp::internal::InterruptedException. So we need to find a way to catch its corresponding event. - The user is supposed to run the subscription in a concurrent instance (not sure which, but some parallelization package of R should be appropriate). In the main script some
unsubscribefunction is called to terminate the concurrent listening. In this case, we need anunsubscribefunction and user documentation is required. -
subscribeprovides a timeout parameter. If no message arrives within the given timeframe, the subscribe function terminates gracefully and stops blocking. I think this a nice-to-have feature. -
subscribeprovides a message-count parameter. Ifnmessages arrived, the subscribe function terminates gracefully and stops blocking. I think this a nice-to-have feature.
Any thoughts on this? Do you think these proposals are sane?
I do not have this problem. You are correct that running subscribe blocks, however when I cancel via ctrl-C in the console I am gracefully returned to the prompt. My session does not crash and I do not lose anything.
Have you tried working in the console? If you have the same issue there we can try to debug further, otherwise it would seem the problem is with RStudio and there's not much we can do to help.
Oh, good idea @johnlaing. I will try it within the next 2 weeks and will provide feedback accordingly. Thank you!
Hm, I pressed Ctrl+C when the subscription was running, but RStudio seems to completely ignore it (in the console). Not sure, how we can improve the situation.
It sounds like you're still in RStudio. When I say "in the console" I mean "outside of Rstudio". Please try that.
@johnlaing Thanks for your response. Sorry, I thought you meant "Console" in RStudio. In RStudio no action is taken if Ctrl+C is pressed and the red stop sign seems to be the only option. Today I had the opportunity to test it on cmd.exe. It works perfectly fine. Ctrl+C aborts the session as desired.

I think I should take this issue upstream to RStudio. Assuming I want to implement option (1) in my list of alternatives above. Thank you.