pub struct ConsumerContext {
pub connection: Arc<WebSocketConnection>,
pub headers: HashMap<String, String>,
pub metadata: HashMap<String, String>,
/* private fields */
}websockets and non-WebAssembly only.Expand description
Consumer context containing connection and message information
This context is passed to WebSocket consumer methods and provides access to:
- The WebSocket connection for sending messages
- HTTP handshake headers (e.g., Cookie, Origin)
- Metadata for storing request-scoped data
- Dependency injection (when the
difeature is enabled)
§HTTP Headers
Headers from the WebSocket HTTP handshake can be stored and retrieved:
let context = ConsumerContext::new(conn)
.with_header("cookie".to_string(), "sessionid=abc123".to_string());
assert_eq!(context.cookie_header(), Some("sessionid=abc123"));Fields§
§connection: Arc<WebSocketConnection>The WebSocket connection
headers: HashMap<String, String>HTTP handshake headers (e.g., Cookie, Origin)
metadata: HashMap<String, String>Additional metadata
Implementations§
Source§impl ConsumerContext
impl ConsumerContext
Sourcepub fn new(connection: Arc<WebSocketConnection>) -> ConsumerContext
pub fn new(connection: Arc<WebSocketConnection>) -> ConsumerContext
Create a new consumer context
§Examples
use reinhardt_websockets::consumers::ConsumerContext;
use reinhardt_websockets::WebSocketConnection;
use tokio::sync::mpsc;
use std::sync::Arc;
let (tx, _rx) = mpsc::unbounded_channel();
let conn = Arc::new(WebSocketConnection::new("conn_1".to_string(), tx));
let context = ConsumerContext::new(conn);Sourcepub fn with_di_context(
connection: Arc<WebSocketConnection>,
di_context: Arc<InjectionContext>,
) -> ConsumerContext
Available on crate feature di only.
pub fn with_di_context( connection: Arc<WebSocketConnection>, di_context: Arc<InjectionContext>, ) -> ConsumerContext
di only.Create a new consumer context with DI context
This constructor is used when dependency injection is needed in WebSocket handlers.
§Examples
use reinhardt_websockets::consumers::ConsumerContext;
use reinhardt_di::{InjectionContext, SingletonScope};
use std::sync::Arc;
let singleton = Arc::new(SingletonScope::new());
let di_ctx = Arc::new(InjectionContext::builder(singleton).build());
let context = ConsumerContext::with_di_context(connection, di_ctx);Sourcepub fn with_header(self, key: String, value: String) -> ConsumerContext
pub fn with_header(self, key: String, value: String) -> ConsumerContext
Add an HTTP handshake header to the context
Header names are stored as-is. For case-insensitive lookup, callers should normalize keys to lowercase before insertion.
Sourcepub fn get_header(&self, key: &str) -> Option<&String>
pub fn get_header(&self, key: &str) -> Option<&String>
Get an HTTP handshake header value
Get the Cookie header from the HTTP handshake
Convenience method equivalent to get_header("cookie").
Sourcepub fn with_metadata(self, key: String, value: String) -> ConsumerContext
pub fn with_metadata(self, key: String, value: String) -> ConsumerContext
Add metadata to the context
Sourcepub fn get_metadata(&self, key: &str) -> Option<&String>
pub fn get_metadata(&self, key: &str) -> Option<&String>
Get metadata value
Sourcepub fn di_context(&self) -> Option<&Arc<InjectionContext>>
Available on crate feature di only.
pub fn di_context(&self) -> Option<&Arc<InjectionContext>>
di only.Get the DI context if available
Sourcepub fn set_di_context(&mut self, ctx: Arc<InjectionContext>)
Available on crate feature di only.
pub fn set_di_context(&mut self, ctx: Arc<InjectionContext>)
di only.Set the DI context
Sourcepub async fn resolve<T>(&self) -> Result<T, WebSocketError>
Available on crate feature di only.
pub async fn resolve<T>(&self) -> Result<T, WebSocketError>
di only.Resolve a dependency with caching
This method extracts the dependency from the DI context. The resolved dependency is cached for the duration of the connection.
§Errors
Returns an error if:
- The DI context is not set
- The dependency cannot be resolved
§Examples
let db: Arc<DatabaseConnection> = ctx.resolve().await?;Sourcepub async fn resolve_uncached<T>(&self) -> Result<T, WebSocketError>
Available on crate feature di only.
pub async fn resolve_uncached<T>(&self) -> Result<T, WebSocketError>
di only.Sourcepub async fn try_resolve<T>(&self) -> Option<T>
Available on crate feature di only.
pub async fn try_resolve<T>(&self) -> Option<T>
di only.Try to resolve a dependency, returning None if DI context is not available
This is useful for optional dependencies or when you want to gracefully handle the case where DI is not configured.
§Examples
if let Some(cache) = ctx.try_resolve::<CacheService>().await {
// Use cache
} else {
// Fallback without cache
}Auto Trait Implementations§
impl Freeze for ConsumerContext
impl !RefUnwindSafe for ConsumerContext
impl Send for ConsumerContext
impl Sync for ConsumerContext
impl Unpin for ConsumerContext
impl UnsafeUnpin for ConsumerContext
impl !UnwindSafe for ConsumerContext
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.