i know basics of c# and asp.net core, however i need some help with event handlers. Im trying to build an asp.net core app which streams tweets to the front-end. Im using tweetinvi library for getting tweets and signalr to do the streaming.
I tested tweetinvi in console application and it worked.I used event handler and got stream of tweets displayed on console. Same approach didnt work on asp.net core application. However i managed to stream randonmly generated numbers to front end. So my js code on front-end and streaming hub setup on back-end worked fine.
namespace TwitterWebApp
{
public class StreamHub : Hub
{
public ChannelReader<string> GetRandomNumber(int count, CancellationToken cancellationToken)
{
var channel = Channel.CreateUnbounded<string>();
StartTwitterStream(channel.Writer, count, cancellationToken);
//_ = StreamRandomNumbers(channel.Writer, 100, cancellationToken);
return channel.Reader;
}
private async Task StreamRandomNumbers(ChannelWriter<string> channelWriter, int count, CancellationToken cancellationToken)
{
var randomNumber = new Random();
for (var i = 0; i < count; i++)
{
cancellationToken.ThrowIfCancellationRequested();
await channelWriter.WriteAsync(Convert.ToString(randomNumber.Next()), cancellationToken);
}
channelWriter.TryComplete();
}
private void StartTwitterStream(ChannelWriter<string> channelWriter, int count, CancellationToken cancellationToken)
{
Auth.SetUserCredentials("xxx", "xxx", "xxx", "xxx");
var stream = Stream.CreateSampleStream();
stream.AddTweetLanguageFilter(LanguageFilter.English);
stream.TweetReceived += async (sender, e) =>
{
var tweet = e.Tweet.Text;
cancellationToken.ThrowIfCancellationRequested();
await channelWriter.WriteAsync(tweet, cancellationToken);
};
stream.StartStream();
}
}
}
Getting tweets requires event handlers implementation in order to stream every tweet once its received. In debugger i see tweet and its content. However line: await channelWriter.WriteAsync(tweet, cancellationToken); doesnt sends strings to front-end. No errors on frontend backend.
I made stream a class variable and checked hashcode and it has the same value. So it looks like stream is not garbaged away and closed.
In debug mode TweetReceived action is called infinitely and generating new tweets, but WriteAsync method does nothing.
Later, in the beggining, when channel is screated i added
channel.Writer.WriteAsync("ok", cancellationToken);
Just to send "ok" string. However this method is not executed, when
stream.StartStream();
is called later. I find it strange why WriteAsync is not working when StartStream is called way later. Removing StartStream makes WriteSync work just fine.
public class StreamHub : Hub
{
public ISampleStream stream;
public ChannelReader<string> GetRandomNumber(int count, CancellationToken cancellationToken)
{
var channel = Channel.CreateUnbounded<string>();
channel.Writer.WriteAsync("ok", cancellationToken);
Auth.SetUserCredentials("xxx", "xxx", "xxx", "xxx");
stream = Stream.CreateSampleStream();
stream.AddTweetLanguageFilter(LanguageFilter.English);
StartTwitterStream(channel.Writer, cancellationToken);
return channel.Reader;
}
private void StartTwitterStream(ChannelWriter<string> channelWriter, CancellationToken cancellationToken)
{
stream.TweetReceived += (sender, e) =>
{
var streamHashCode1 = sender.GetHashCode();
var tweet = e.Tweet.Text;
cancellationToken.ThrowIfCancellationRequested();
channelWriter.WriteAsync(tweet, cancellationToken);
};
var streamHashCode2 = stream.GetHashCode();
stream.StartStream();
}
}