Exploring SSE with Rust and Tide by streaming tweets
Hi there! welcome back, last time we explore how to use WebSocket in tide and this time we will exploring how to use Server Sent Events.
SSE are a lightweight alternative to web-sockets if you only needs to send updates from the server.
Tide not only have built in support for sse but also have an example
we can run, so let’s start by running that example.
The simplest way to run an example is to clone the repo and run
cargo run --example <example name>
|
|
And in another terminal
|
|
Nice! that works as expected, you can see the content-type text/event-stream
. Now, we can check the code of the tide example
…
|
|
As you can see, in this example is defined and endpoint /sse
and the closure
receive two arguments, the request
and a sender
that is used after to send
information.
This is a great example! and give us the basics to explore how to use sse
in other scenarios.
As the title
says, our goal is stream tweets using sse
and in this first exploration we will try to mimic the behavior of the socket.io home page but with sse
and tracking rust
and http-rs
words.
Create the application structure
Let’s start by create the application and set the deps and the basic struct
|
|
Now, enable the attributes
for async-std
in your Cargo.toml
since we will need those.
|
|
Like in the other
tide
example apps we will be using skeleton to have a nice styling, so you can go ahead and put the required files in/public/css
.
Connect the dots…
Let’s start wit some rust
code :), in our main.rs
file let create the basic structure of our application.
|
|
Here we are creating the tide
server with an index
route, note that serve_file
method were added in tide v0.16.0 release, and we are using dotenv
to load the env vars
from the .env
file.
Nice! we have the basic structure in place, and we leave the sse endpoint
blank to implement the logic to stream tweets
.
We will using the crate
twitter-stream
to connect us to the twitter api and track out topics.
As we saw in the first example, the sse
endpoint give us a sender
to send data to the client, so here we need to some how connect this sender with the tweets we receive from the twitter api. For this purpose we will use a broadcast channel
that allow us to broadcast the received tweet to all clients.
First we need to create the broadcast channel
and store in our State
for make it available in every request.
|
|
And now we can listen in our sse
endpoint
|
|
Here we are listen tweets from the broadcaster
and send them through the sse connection
to the clients, we are also using a Tweet
struct ( and a User ) to serialize the fields we want to use and stream.
|
|
Great! we already have one side of the channel, now we need to code the other side. We will create a fn
that spawn
a new task
for tracking the topic from twitter and send through the broadcast channel.
|
|
So, we are receiving the broadcaster as argument and the we use inside of the spawned
task to notify that we receive a new tweet
.
Awesome!! we just connected the dots… time to run
(cargo run) and see it in action….
That’s all for today, we copy the base funtionality of the socket.io home page but using sse
with Tide :). In the next post
we will extend this application to allow users to create their own trend tracks
. You can check the repo for the complete version and check the rust trends
As always, I write this as a learning journal and there could be another more elegant and correct way to do it and any feedback is welcome.
Thanks!
Author Javier Viola
LastMod 2021-02-08