This is the code I used during my Spring Webflux presentation at the Charlotte Java User Group on 6/14/17.
In short, this codes builds an application that connects to the Twitter Streaming API to retrive a stream of Tweets, the tweets are read using Spring's WebClient, stored in a MongoDB using Spring Data's new ReactiveCrudRepository and finally exposed as a REST point that sends Server Sent Events as we get new Tweets real time, all of these using a Reactive programming model.
Helps to create the Twitter Authorization signature, heavily based and dependent on Twitter's Hosebird Client (hbc). Don't use this for any other purpose than this demo, is ugly, I just built this to save time during the live coding part of the presentation.
This is the actual application that does the work
I'm using MongoDB for this example, so you'll need this up and running in your local machine, if you are on OSX, you can easily install MongoDB using Homebrew
You need to create a Twitter App, this is free, just go to Twitter Apps
- Create an application-secret.properties file under
webflux-twitter-app/src/main/resources
that looks like the below, use the values from your created Twitter application
twitter.consumer-key=<Your Twitter App's Consumer Key (API Key)>
twitter.consumer-secret=<Your Twitter App's Consumer Secret (API Secret)>
twitter.token=<Your Twitter App's Access Token>
twitter.secret=<Your Twitter App's Access Token Secret>
-
For exposing the endpoint as an infinite stream of Server Sent Events we need to create a tailable cursor to the Tweets collection, in order to do that we need a Capped Collection in MongoDB, so we'll need to create this manually, you can create it using the below commands after connecting to the Mongo shell.
- Create and start using the cltjug database with the command
use cltjug
- Create the collection with something like
db.createCollection("tweets", {capped:true, max:1500, size:1000000})
- Create and start using the cltjug database with the command
-
Time to compile and build,
mvn clean install
- Below an example of how to run the program, every argument will be used as a filter track, if no arguments are present a default filter will be used. Tracks can be hastags, twitter handler names or just simply some text.
java -jar webflux-twitter-app/target/webflux-twitter-app-0.0.1-SNAPSHOT.jar "#cltjug" "webflux" "@reactor"
If everything goes well you will start seeing the Tweets in the console, you can curl the tweets enpoint at http://localhost:8080/tweets
A thymeleaf template is included with enough Javascript to connect to the endpoint and display the tweets, you can open it at http://localhost:8080/
Just remember the Tweets are real time and not historic, as soon as the application connects you will get only new Tweets real time, based on the filters passed, you can try to use any trending hashtag to see a lot of activity or you can use your own and tweet to be able to see your own tweets arriving real time