Tweetinvi STREAM works on console application but It does not work in Web Application

Apr 16, 2015 at 5:05 AM
Hi everyone,

I'm using tweetinvi to get data from filtered stream, I'm using signalR to expose this data while this is working good in a console application I could not get this working in ASP.NET 4.5 appliaction.
I created a Web Application with the Empty template and I just add SignalRClass(v2) and a StartUp OWIN class. Then I create a simple html page to consume that. While everything is fine when I called Tweetinvi from SignalR hub in console application, same thing it does not work in the web app. If somebody could help I appreciate a lot :)

Thanks in advance

Camilo
Coordinator
Apr 19, 2015 at 1:05 AM
As I said in a post recently, it has been a while that I have not worked with SignalR.

But what you need to understand is that your webserver needs to be running this long Stream operation. Therefore it means that when a client disconnect from your webserver, your hub must continue to work in order to keep processing the data.

Sorry for not being able to give you more help. I will get back on Web Application soon but as far as right now this is the only advice I can give you.

Linvi
Apr 19, 2015 at 2:31 PM
Edited Apr 19, 2015 at 2:40 PM
Having had experience of this.. SignalR Hub classes are transient meaning that Hub instances are created each time a client creates a connection with SignalR. So the Hub class is not the place to put the Tweetinvi code.

As you are only allowed to have one (filtered) streaming instance open to the API at any time, you need to ensure that only one instance of the stream is created in the lifetime of the application. A quick and dirty way of getting this working is to make up a static class where you put all of your Tweetinvi code and put all of the connecting to Twitter code into a static method and then use Task.Factory.StartNew in your Global.asax file to start up the work:

// Create Task for the stream of tweets
Task.Factory.StartNew(() => TwitterStream.Setup());

Full code here: https://github.com/adaam2/TwitterTrendingMap/blob/master/TwitterLogic/TwitterStream.cs

n.b. there's probably a better, cleaner way of doing this.
Coordinator
Apr 19, 2015 at 4:50 PM
Thanks adaam for your contribution. I think this is quite relevant.
Some people might have other ideas but I think this is a good way to do.

Cheers,
Linvi
Apr 20, 2015 at 2:29 AM
First of all, thanks linvi and adaam2 for your replay and help. As adaam2 said, I read deeply the SignalR docs and found (and hope understand :)) that transient class concept; then I put my code in a separate class, but I still did not get the stream; things were going better when I start using async methods on Tweetinvi. I replace the StartStreamMatchingAllConditions() with StartStreamMatchingAllConditionsAsync(); and tweets start to get pushing in good shape through signalR Hub to web page.
The problem I'm facing now is I could not stop the stream from the client. I sent the message to the server I saw it in logs but it has never reached the Hub code.
Thanks again.
Coordinator
Apr 20, 2015 at 5:10 PM
Hi,

What do you mean by it has never reached the Hub Code.

As Adam said, the hub are transient. Therefore you need to make your hub communicate with your static class.

Have you checked that your static class receives the request to stop the stream?
Do you mean Tweetinvi is not stopping the stream whilst your request it to?

Linvi