Can not open ws connection to outside of local -tokio-tungstenite

use futures::{SinkExt, StreamExt};
use std::{net::SocketAddr, error,};
use tokio::net::{TcpListener, TcpStream};
use tokio_tungstenite::{accept_async, tungstenite::Error,tungstenite::Result};

async fn accept_connection(peer: SocketAddr, stream: TcpStream) {
    if let Err(e) = handle_connection(peer, stream).await {
        match e {
            Error::ConnectionClosed | Error::Protocol(_) | Error::Utf8 => println!("Conn closed"),
            err => println!("Error processing connection: {}", err),

async fn handle_connection(peer: SocketAddr, stream: TcpStream) -> Result<()> {
    let  ws_stream = accept_async(stream).await.expect("Failed to accept");
    let (mut send,mut read) = ws_stream.split();
    println!("New WebSocket connection: {}", peer);

    while let Some(msg) = {
        let msg = msg?;
        if msg.is_text() || msg.is_binary() {


async fn main() -> Result<()>{

    let addr = "";
    let listener = TcpListener::bind(&addr).await.expect("Can't listen");
    println!("Listening on: {}", addr);

    while let Ok((stream, _)) = listener.accept().await {
        let peer = stream.peer_addr().expect("connected streams should have a peer address");
        println!("Peer address: {}", peer);

        tokio::spawn(async move  {accept_connection(peer, stream).await;});

Hey everyone im trying to open connection to outside of local (works fine in local) but couldunt manage it.
Used aws ligtsail ubuntu 18.0 and also did port forwarding as Custom TPC for 3001 and still clients cant connect getting Connection Refused error.
What am i missing ?

Binding on doesn't work for traffic coming from outside the machine. You can use to accept connections from any IP, or you can check what the servers public IP is and bind to that.

2 Likes didint work. Still getting problem but i dont understand must be working cuz i was doing same on my nodejs servers and was binding to
Error message is Failed to connect: Io(Os { code: 10061, kind: ConnectionRefused, message: "Could not connect because the target machine actively refused." })

Weird thing is that im doing same thing with nodejs socketio server and it works but with rust i couldunt open outside of local (works fine in local but need to open to outside )
here is the example code that i use:

Do you have a reverse proxy (nginx, apache, etc) set up in front of your node program? That would make work.

nah only used socketio here is the code that i used for nodejs i posted it to stackoverflow and after struggling a bit found the solution by changing from windows to linux.

This example code works perfectly for new connections outside of local.(nodejs)

Your node code isn't listening on it's only specifying a port, and it's using a different port from your rust code. Are you sure you have everything set up to use the correct port?

eyo i checked again and seems did something wrong while deploy yeah first u said was correct thanks a lot brother.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.