-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add server side events to notify the clients #346
base: main
Are you sure you want to change the base?
Changes from 5 commits
c210e48
c59b438
f29db9d
e8c552f
1c110b2
02cd53d
9f77531
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::sync::{Arc, Mutex}; | ||
use actix_web_lab::sse; | ||
use actix_web_lab::sse::Event; | ||
Check failure on line 3 in server/src/sse.rs
|
||
use futures_util::SinkExt; | ||
Check failure on line 4 in server/src/sse.rs
|
||
use log::{info, log}; | ||
Check failure on line 5 in server/src/sse.rs
|
||
use tokio::sync::mpsc; | ||
use tokio::sync::mpsc::{channel, Receiver}; | ||
Check failure on line 7 in server/src/sse.rs
|
||
use tokio_stream::wrappers::ReceiverStream; | ||
use serde::Serialize; | ||
|
||
#[derive(Serialize)] | ||
pub struct SseMessage { | ||
pub(crate) status: String, | ||
pub(crate) node: NodeInfo, | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct NodeInfo { | ||
pub(crate) id: String, | ||
pub(crate) peer: String, | ||
pub(crate) seen: String, | ||
#[serde(rename = "addrStatus")] | ||
pub(crate) addr_status: String, | ||
} | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct SseClients{ | ||
clients: Arc<Mutex<Vec<mpsc::Sender<sse::Event>>>> | ||
} | ||
|
||
impl SseClients{ | ||
pub fn new() -> Self{ | ||
SseClients{ | ||
clients: Arc::new(Mutex::new(Vec::new())), | ||
} | ||
} | ||
|
||
|
||
|
||
pub async fn add_client(&self) -> ReceiverStream<sse::Event> { | ||
let (tx, rx) = mpsc::channel(10); | ||
|
||
// Send a "connected" message to the new client | ||
tx.send(sse::Data::new("connected").into()).await.unwrap(); | ||
|
||
// Add the sender to the list of clients | ||
self.clients.lock().unwrap().push(tx); | ||
info!("New SSE connection established"); | ||
|
||
// Return the receiver stream to be used for SSE | ||
ReceiverStream::new(rx) | ||
} | ||
|
||
pub fn get_no_of_clients(&self) -> usize{ | ||
self.clients.lock().unwrap().len() | ||
} | ||
|
||
pub async fn broadcast(&self, msg:&str){ | ||
let clients = self.clients.lock().unwrap().clone(); | ||
let send_futures = clients.iter().map(|client| client.send(sse::Data::new(msg).into())); | ||
let _ = futures_util::future::join_all(send_futures).await; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix those unused imports. Changes look fine otherwise, I’ll approve once this is fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the build fails without those 3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, the comment might’ve been placed on the wrong line. See the checks tab — it says which imports are unused (also not all code is formatted with cargo fmt, this too needs to be done)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code formatted and removed unused imports