How to Send In-App Notifications with Rust

Web development traditionally follows a Request-Response model, where the browser sends a request to the server, and the server responds. This model is inherently one-sided. However, modern applications often require that the server be able to send req…


This content originally appeared on Level Up Coding - Medium and was authored by Shivam Mathur

Web development traditionally follows a Request-Response model, where the browser sends a request to the server, and the server responds. This model is inherently one-sided. However, modern applications often require that the server be able to send requests to the client when business requirements dictate.

There are three primary methods to address this need: WebSockets, WebTransport, and Server-Sent Events (SSE).

Since WebTransport is currently not supported in Safari, we’ll skip that for now.

Generated by DALL-E 3

WebSockets

WebSocket is a protocol that operates over traditional HTTP. After the initial handshake, the connection remains open instead of closing after a single request-response cycle.

If you’ve followed any Node.js tutorials around 2013, you might have started with creating a chat app using Socket.io, which runs on top of WebSockets.

Server-Sent Events (SSE)

Server-Sent Events maintain an open HTTP connection with the content type set to text/event-stream. They enable unidirectional communication, allowing the server to push events to clients that have opened a connection.

This is further facilitated by a JavaScript interface: EventSource. For more information, check out the MDN documentation.

Notifications with Rust and Axum

I’ve created an implementation of both WebSockets and Server-Sent Events in Rust, which you can find here. Rust’s unique language features, such as the borrow checker, enforce safety by restricting variable access. In this implementation, I’ve created a broadcast channel that allows multiple producers and consumers. This channel is shared as server state in the Axum server, making it accessible to every API endpoint.

There is an API endpoint /admin/send-notifications/:user_id that publishes notifications to the broadcast channel. The WebSocket (/ws/:user_id) and SSE (/sse/:user_id) endpoints receive these broadcast messages and forward them to the connected client if the message is intended for that user.

WebSocket Implementation

In the WebSocket implementation, we split the sending and receiving parts of the WebSocket into two Tokio tasks. Since we only care about notifications, the receiver task checks for a Ping from the client and responds with a Pong into the broadcast channel. The sender task monitors the broadcast channel for new messages and forwards them to the client.

Server-Sent Events Implementation

The SSE implementation is more straightforward. We convert the broadcast receiver into a stream, filter it for messages intended for the connected user, format these messages into events, and send the stream to the client.

This approach ensures that the server can send real-time notifications to clients using Rust and Axum, leveraging the strengths of both WebSockets and Server-Sent Events.


How to Send In-App Notifications with Rust was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Shivam Mathur


Print Share Comment Cite Upload Translate Updates
APA

Shivam Mathur | Sciencx (2024-07-23T15:46:45+00:00) How to Send In-App Notifications with Rust. Retrieved from https://www.scien.cx/2024/07/23/how-to-send-in-app-notifications-with-rust/

MLA
" » How to Send In-App Notifications with Rust." Shivam Mathur | Sciencx - Tuesday July 23, 2024, https://www.scien.cx/2024/07/23/how-to-send-in-app-notifications-with-rust/
HARVARD
Shivam Mathur | Sciencx Tuesday July 23, 2024 » How to Send In-App Notifications with Rust., viewed ,<https://www.scien.cx/2024/07/23/how-to-send-in-app-notifications-with-rust/>
VANCOUVER
Shivam Mathur | Sciencx - » How to Send In-App Notifications with Rust. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/23/how-to-send-in-app-notifications-with-rust/
CHICAGO
" » How to Send In-App Notifications with Rust." Shivam Mathur | Sciencx - Accessed . https://www.scien.cx/2024/07/23/how-to-send-in-app-notifications-with-rust/
IEEE
" » How to Send In-App Notifications with Rust." Shivam Mathur | Sciencx [Online]. Available: https://www.scien.cx/2024/07/23/how-to-send-in-app-notifications-with-rust/. [Accessed: ]
rf:citation
» How to Send In-App Notifications with Rust | Shivam Mathur | Sciencx | https://www.scien.cx/2024/07/23/how-to-send-in-app-notifications-with-rust/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.