<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<atom:link href="https://seanbehan.ca/posts/tag/rust/rss.xml" rel="self" type="application/rss+xml" />
		<title>Sean Behan — Rust</title>
		<link>https://seanbehan.ca/posts/tag/rust</link>
		<description>Posts tagged “Rust”.</description>
		<language>en-CA</language>
		<!-- RSS wants an address here and readers show the name beside it. -->
		<managingEditor>sean@seanbehan.ca (Sean Behan)</managingEditor>
		<webMaster>sean@seanbehan.ca (Sean Behan)</webMaster>
		<lastBuildDate>Sun, 13 Sep 2026 02:49:38 GMT</lastBuildDate>
		<item>
		<guid isPermaLink="true">https://seanbehan.ca/posts/websockets-rust</guid>
		<title><![CDATA[WebSockets in Rust]]></title>
		<description><![CDATA[Part one of a Rust series: a WebSocket server built on the ws crate.]]></description>
		<link>https://seanbehan.ca/posts/websockets-rust</link>
		<pubDate>Wed, 27 Nov 2019 02:56:22 GMT</pubDate>
		<category>programming</category><category>Rust</category><category>WebSocket</category>
		<content:encoded><![CDATA[<h3>Introduction</h3><p>Today I&#x27;ll show you how you can write incredibly fast code in Rust. This is part one of a tutorial series.</p><p>This is an intermediate tutorial, if you are unfamiliar with Rust basics I would suggest reading the Rust book for free at <a href="https://doc.rust-lang.org/stable/book/">https://doc.rust-lang.org/stable/book/</a>.</p><h3>Project Setup</h3><p>Lets dive right in.</p><pre><code class="language-bash">cargo new rust-tutorial
cd rust-tutorial</code></pre><p>Let&#x27;s build our websocket server.</p><p>Edit your <code>Cargo.toml</code> and add <a href="https://docs.rs/ws/">`ws`</a> to your <code>[dependencies]</code>.</p><p>Your <code>Cargo.toml</code> should now look something like this.</p><pre><code class="language-toml">[package]
name = "rust-tutorial"
version = "0.1.0"
authors = ["Sean Behan &lt;codebam@riseup.net&gt;"]
edition = "2018"

[dependencies]
ws = "*"</code></pre><h3>Building the WebSocket Server</h3><p>Import our libraries.</p><pre><code class="language-rust">use ws::listen;</code></pre><p>Start a websocket listener.</p><pre><code class="language-rust">fn main() {
    listen("127.0.0.1:5000", |out| {
        move |msg: ws::Message| {
            // handle the msg here
        }
    }
}</code></pre><h3>Handling Messages</h3><p>We&#x27;ll respond to recieved messages and just echo them back for now.</p><p>Of course you could easily pass this data into any function and use it to generate a response. In fact the incoming and outgoing data doesn&#x27;t even have to be text.</p><pre><code class="language-rust">out.send(format!("recieved message: {}", msg.into_text().unwrap())).unwrap();
out.close(ws::CloseCode::Normal)</code></pre><h3>Testing the Server</h3><p>If we connect to this now using websocat, which can be installed with `cargo install websocat`, we can see the server echos back messages that we send to it.</p><pre><code class="language-sh">$ websocat ws://127.0.0.1:5000
hello world
recieved message: hello world</code></pre><p>This isn&#x27;t that interesting. It&#x27;s cool that we can do all this in just 8 lines of Rust though!</p><h3>Next Steps</h3><p>In the next part of this series I&#x27;ll show you how to use use <a href="https://docs.rs/tokio/">`tokio`</a> to create an asyncronous server so we can manage simultaneous connections. Stay tuned!</p>]]></content:encoded>
	</item>
	</channel>
</rss>