Note that there are some explanatory texts on larger screens.

plurals
  1. POSignalr Hubs & Twitter Streaming
    text
    copied!<p>So I'm trying to use Signalr with the Twitter Streaming API, and specifically, for this I'm using the Tweetinvi C# API (<a href="http://tweetinvi.codeplex.com/" rel="nofollow">http://tweetinvi.codeplex.com/</a>).</p> <p>The purpose of the app is to stream tweets to a page in realtime filtered with certain keywords. </p> <p>The TweetInvi library works a treat, and I have a command line application successfully printing out tweets with certain keywords in.</p> <p>The basic outline of my usage is as follows:</p> <p>I have an MVC web app with a single page, with a text input and a button (for updating filters) it then calls the Hub method in the Signalr Hub, to start the stream if there isn't one already present and Stops it on a second button click.</p> <p>All this is working fine, except when it comes to the signalr part.</p> <pre><code>public class TweetHub : Hub { private IStreamManager _streamManager; public void AddTweet(String tweet, double lat, double lon) { Clients.All.addTweet(tweet, lat, lon); } public void StartStream(String[] filters) { string accessToken = ConfigurationManager.AppSettings["AccessToken"]; string accessTokenSecret = ConfigurationManager.AppSettings["AccessTokenSecret"]; string consumerKey = ConfigurationManager.AppSettings["ConsumerKey"]; string consumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"]; IToken token = new Token(accessToken, accessTokenSecret, consumerKey, consumerSecret); if (_streamManager != null &amp;&amp; _streamManager.StreamIsOpen()) { _streamManager.StopStream(); _streamManager.StartStream(token, filters, tweet =&gt; AddTweet(tweet.Text, tweet.LocationCoordinates.Lattitude, tweet.LocationCoordinates.Longitude)); } else if (_streamManager != null &amp;&amp; !_streamManager.StreamIsOpen()) { _streamManager.StartStream(token, filters, tweet =&gt; AddTweet(tweet.Text, tweet.LocationCoordinates.Lattitude, tweet.LocationCoordinates.Longitude)); } else { _streamManager = new StreamManager(); _streamManager.StartStream(token, filters, tweet =&gt; AddTweet(tweet.Text, tweet.LocationCoordinates.Lattitude, tweet.LocationCoordinates.Longitude)); } } public void StopStream() { if (_streamManager != null &amp;&amp; _streamManager.StreamIsOpen()) { _streamManager.StopStream(); } } } </code></pre> <p>That is the code for my Signalr Hub. As I said, using js I can trigger the start and stop stream methods fine.</p> <p>This is the code for my StreamManager class:</p> <pre><code>public class StreamManager : IStreamManager { private StreamClient _streamClient; private bool _streamOpen = false; public void StartStream(IToken token, String[] filters, Action&lt;ITweet&gt; action) { if (_streamClient == null) _streamClient = new StreamClient(token, filters, new FilteredStream()); _streamClient.StartStream(action); _streamOpen = true; } public void StopStream() { if (_streamClient != null) { _streamClient.StopStream(); _streamOpen = false; } } public bool StreamIsOpen() { return _streamOpen; } public void Dispose() { if (_streamOpen) { StopStream(); } _streamClient.Dispose(); _streamClient = null; } } </code></pre> <p>The code for my StreamClient class:</p> <pre><code>public class StreamClient : IStreamClient { private IFilteredStream _filteredStream; private IToken _token; private bool _streamOpen = false; public StreamClient(IToken token, String[] filters, IFilteredStream filteredStream) { _token = token; _filteredStream = filteredStream; AddFilters(filters); } private void AddFilters(String[] filters) { for (int i = 0; i &lt; filters.Length; ++i) { _filteredStream.AddTrack(filters[i]); } } public void StartStream(Action&lt;ITweet&gt; action) { _filteredStream.StartStream(_token, action); _streamOpen = true; } public void StartStream(Func&lt;ITweet, bool&gt; predicateFunc) { _filteredStream.StartStream(_token, predicateFunc); _streamOpen = true; } public void StopStream() { _filteredStream.StopStream(); _streamOpen = false; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { // free managed resources if (_streamOpen) { _filteredStream.StopStream(); _filteredStream = null; _token = null; } } } </code></pre> <p>This code above, is where it makes a call to the Tweetinvi library directly.</p> <p>My problem is that when I pass the Hub method into the StreamManager's StartStream method as an Action parameter, the AddTweet method never gets hit.</p> <p>As I said, this all works fine, when using a command prompt application as a client instead, and using this code:</p> <pre><code>static void Main(string[] args) { string accessToken = ConfigurationManager.AppSettings["AccessToken"]; string accessTokenSecret = ConfigurationManager.AppSettings["AccessTokenSecret"]; string consumerKey = ConfigurationManager.AppSettings["ConsumerKey"]; string consumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"]; IToken token = new Token(accessToken, accessTokenSecret, consumerKey, consumerSecret); String[] filters = new string[2] { "test", "twitter" }; StreamClient streamClient = new StreamClient(token, filters, new FilteredStream()); streamClient.StartStream(tweet =&gt; TestMethod()); } public static void TestMethod() { Console.WriteLine("test"); } </code></pre> <p>This works perfectly and prints out tweets with those keywords as they are received.</p> <p>This leads me to believe that is a problem with the way I am using Signalr, that the signalr method is never getting hit, because the stream definitely gets opened, I just have a sneaky suspicion that it is something to do with the lifetime of the hub and the way I am using it.</p> <p>I suspect this because, although the StartStream Method in my Hub gets called fine, and updates the button being clicked, when I think click again to call StopStream, the StopStream method gets hit, but my "_streamManager" member variable is null, which it shouldn't be IF the hub maintains state during it's lifetime, which I guess it doesn't.</p> <p>Either that or it's being disposed of and then the stream wouldnt exist anymore anyway.</p> <p>I don't really have enough experience with Signalr to properly debug.</p> <p>Thanks in advance for any help. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload