You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am building a Rust-based service using the ntex framework, and my goal is to notify specific WebSocket clients in real-time when a REST API endpoint is triggered. Below are the key features I want to achieve:
Features:
WebSocket Clients: Clients connect to the /ws/{machine_id} endpoint, where machine_id uniquely identifies each client.
REST API Trigger: A REST API endpoint (/notify-machine) will be called with a machine_id and a message. This should notify the WebSocket client associated with that machine_id in real time.
Shared State: I need to maintain an in-memory registry to map machine_id to WebSocket connections for efficient message delivery.
Current Setup:
WebSocket Connection: Implemented a WebSocket service with heartbeat monitoring using web::ws::WsSink and state management.
REST API: The goal is to trigger message delivery to WebSocket clients from the REST API handler.
Below is a simplified version of my current implementation:
Mapping Machine ID to Connections: How do I efficiently map machine_id to WebSocket connections (WsSink) for targeted communication?
Sending Messages: How do I send a message to a specific WebSocket client (machine_id) when the /notify-machine REST API is triggered?
State Management: What is the best approach to maintain the state of connected clients and their associations with machine_id?
Desired Behavior:
When a client connects to /ws/{machine_id}, the connection should be registered with its machine_id.
When the /notify-machine endpoint receives a POST request with a machine_id and a message, the corresponding WebSocket client should receive that message in real time.
The WebSocket clients are behind NAT, so I cannot use HTTP to reach them. WebSockets are the only viable solution for real-time communication.
I am building a Rust-based service using the ntex framework, and my goal is to notify specific WebSocket clients in real-time when a REST API endpoint is triggered. Below are the key features I want to achieve:
Features:
/ws/{machine_id}
endpoint, wheremachine_id
uniquely identifies each client./notify-machine
) will be called with amachine_id
and a message. This should notify the WebSocket client associated with thatmachine_id
in real time.machine_id
to WebSocket connections for efficient message delivery.Current Setup:
web::ws::WsSink
and state management.Below is a simplified version of my current implementation:
WebSocket Service:
REST API and State Management:
Key Issues:
machine_id
to WebSocket connections (WsSink
) for targeted communication?machine_id
) when the/notify-machine
REST API is triggered?machine_id
?Desired Behavior:
/ws/{machine_id}
, the connection should be registered with itsmachine_id
./notify-machine
endpoint receives a POST request with amachine_id
and a message, the corresponding WebSocket client should receive that message in real time.What I’ve Tried:
I referred to [ntex examples](https://github.com/ntex-rs/examples), but did not help. I am unsure how to manually send messages to specific clients from the REST API. For instance, in this [example snippet](https://github.com/anchalshivank/mt-notify-service/blob/8ca284333eb44eeadd8568631b4e86d61e7b03a8/src/main.rs#L59), I see how to send responses, but I need help sending messages proactively to connected clients.
Any guidance, examples, or advice for achieving this functionality would be greatly appreciated!
The text was updated successfully, but these errors were encountered: