Executing send_message from Actix Websocket Chat example


I have successfully implemented the following Websocket chat example. I don't want to make a chat though, but just be able to send a push notification to a specific admin whenever a new order is placed on my platform.

I was thinking I could make a 'chat room' for each admin (this will be a few hundred admins). And when an order is placed I put a notification in that chatroom.

I am getting close, but I still need to figure out on how to send a message through the websocket from any place in my code.

server.rs has the following code:

/// Handler for Message message.
impl Handler<ClientMessage> for ChatServer {
    type Result = ();

    fn handle(&mut self, msg: ClientMessage, _: &mut Context<Self>) {
        self.send_message(&msg.room, msg.msg.as_str(), msg.id);

I am trying to execute send_message from another place in my code. This is my attempt:

async fn send_test(srv: web::Data<actix::Addr<websocket_server::ChatServer>>) {
    srv.get_ref().send_message("main", "test message", 1);


error[E0599]: no method named `send_message` found for reference `&actix::Addr<ChatServer>` in the current scope
    --> src/main.rs:1161:19
1161 |     srv.get_ref().send_message("main", "test message", 1);
     |                   ^^^^^^^^^^^^ method not found in `&actix::Addr<ChatServer>`

For more information about this error, try `rustc --explain E0599`.

How would I continue from here?


This is the solution:

async fn send_test(
    srv: web::Data<actix::Addr<websocket_server::ChatServer>>,
) -> Result<HttpResponse> {
    srv.send(websocket_server::ClientMessage {
        id: 0,
        msg: "Test 123".to_string(),
        room: "main".to_string(),