11

I've tried using different libraries and different implementations but I haven't been able to get a working WebSocket client/ listener in rust.

I tried writing a handler:

extern crate ws;

use ws::{connect, listen, Handler, Sender, Handshake, Result, Message, CloseCode};

struct Client {
    out: Sender,
}

impl Handler for Client {
    fn on_open(&mut self, _: Handshake) -> Result<()> {
        self.out.send(r#"{"action": "authenticate","data": {"key_id": "<API_KEY>","secret_key": "<API_SECRET>"}}"#);
        self.out.send(r#"{"action": "listen","data": {"streams": ["AM.SPY"]}}"#)
    }

    fn on_message(&mut self, msg: Message) -> Result<()> {
        println!("message: {}", msg);
        Ok(())
    }
}

fn main() {
    if let Err(error) = listen("wss://data.alpaca.markets/stream", |out| {
        Client { out: out }
    }) {
        println!("Failed to create WebSocket due to: {:?}", error);
    }
}

And I tried this too:

extern crate ws;

use ws::{connect, CloseCode};

fn main() {
    if let Err(error) = connect("wss://data.alpaca.markets/stream", |out| {
        if out.send(r#"{"action": "authenticate","data": {"key_id": "<API_KEY>","secret_key": "<API_SECRET>"}}"#).is_err() {
            println!("Websocket couldn't queue an initial message.")
        } else {
            println!("Client sent message 'Hello WebSocket'. ")
        };

        if out.send(r#"{"action": "listen","data": {"streams": ["AM.SPY"]}}"#).is_err() {
            println!("Websocket couldn't queue an initial message.")
        } else {
            println!("Client sent message 'Hello WebSocket'. ")
        };

        move |msg| {
            println!("message: '{}'. ", msg);

            Ok(())
        }
    }) {
        println!("Failed to create WebSocket due to: {:?}", error);
    }
}

To make sure that the connection I was trying to connect to wasn't the problem I wrote the same code in JS. This does work.

const ws = require("ws");

const stream = new ws("wss://data.alpaca.markets/stream");

stream.on("open", () => {
    stream.send('{"action": "authenticate","data": {"key_id": "<API_KEY>","secret_key": "API_SECRET"}}');
    stream.send('{"action": "listen","data": {"streams": ["AM.SPY"]}}');
});

stream.on("message", (bar) => {
    process.stdout.write(`${bar}\n`);
});

In both instances of the rust code the code compiles and runs but the on_open function and the lambda function is never called.

Thank you in advance.

Loading
  • 426
  • 1
  • 5
  • 11
  • Did you try connecting to another websocket server (and sending it a simple message), just to see if your libraries work at all? There could be some issue specific to the server you're trying to connect. – user4815162342 Nov 11 '21 at 20:44
  • I had taken a break from the project but I fixed it now, I got it to work with tungstenite – Loading Jan 13 '22 at 03:43

3 Answers3

32

To anyone who is facing this same issue I would recommend using tungstenite and for async websockets tokio-tungstenite

This is the code that ended up working for me:

use url::Url;
use tungstenite::{connect, Message};

let (mut socket, response) = connect(
    Url::parse("wss://data.alpaca.markets/stream").unwrap()
).expect("Can't connect");

socket.write_message(Message::Text(r#"{
    "action": "authenticate",
    "data": {
        "key_id": "API-KEY",
        "secret_key": "SECRET-KEY"
    }
}"#.into()));

socket.write_message(Message::Text(r#"{
    "action": "listen",
    "data": {
        "streams": ["AM.SPY"]
    }
}"#.into()));

loop {
    let msg = socket.read_message().expect("Error reading message");
    println!("Received: {}", msg);
}

And this in the Cargo.toml:

[dependencies]
tungstenite = {version = "0.16.0", features = ["native-tls"]}
url = "2.2.2"

The problem I was facing was that the methods I was using were not meant for TLS streams but instead TCP streams. With tungstenite if you enable the native-tls feature both TCP and TLS streams are handles properly by the connect method.

Loading
  • 426
  • 1
  • 5
  • 11
  • 2
    Thank you for posting this. It helped me very much. – Nate Houk Feb 01 '22 at 19:08
  • Hi Loading I am just copy your code to test and get the result "thread 'main' panicked at 'Can't connect: Tls(Native(Ssl(Error { code: ErrorCode(1), cause: Some(Ssl(ErrorStack([Error { code: 336134278, library: "SSL routines", function: "ssl3_get_server_certificate", reason: "certificate verify failed", file: "s3_clnt.c", line: 1264 }]))) }, X509VerifyResult { code: 10, error: "certificate has expired" })))', src/main.rs:7:7 "; is there something wrong? – Henry Mar 02 '23 at 07:41
0

How about tokio_tungstenite?

use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::Message;


let strurl = format!("{}/ws?listenKey={}", WSS_URL, &listenkey);
let url = Url::parse(&strurl)?;
let (mut socket, _) = connect_async(url).await?;

let msg_tobe_send = Message::text("{\"method\":\"ping\"}").into();

loop {
  socket.send(msg_tobe_send).await?;
  let message = socket.next().await.ok_or("No response from server")??;
  println!("Received message: = {:?}", message);
}

At the line of send, got the error of below:

no method named send found for struct WebSocketStream in the current scope items from traits can only be used if the trait is in scope

Nicole
  • 127
  • 1
  • 1
  • 7
0

Look at the imports the StreamExt and SinkExt traits is required for send to be a function. The below code works. I want to suggest that the tokio-tungstenite documentation is really bad for beginner and the below took me ages to find. tokio-tungstenite devs please update your crate with more information and examples.

Credits to https://github.com/snapview/tokio-tungstenite/issues/137#issuecomment-732018849

use tokio::io::{AsyncWriteExt, Result};
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
use futures_util::{StreamExt, SinkExt};

#[tokio::main]
pub async fn main() -> Result<()> {
    println!("Hello, tokio-tungstenite!");

    let url = url::Url::parse("wss://ws.kraken.com").unwrap();

    let (ws_stream, _response) = connect_async(url).await.expect("Failed to connect");
    println!("WebSocket handshake has been successfully completed");

    let (mut write, read) = ws_stream.split();

    println!("sending");

    write.send(Message::Text(r#"{
        "event": "ping",
        "reqid": 42
      }"#.to_string()+"\n")).await.unwrap();

    println!("sent");

    let read_future = read.for_each(|message| async {
        println!("receiving...");
         let data = message.unwrap().into_data();
         tokio::io::stdout().write(&data).await.unwrap();
         println!("received...");
    });

    read_future.await;

    Ok(())
}
Fanisus
  • 13
  • 3