<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sacha Arbonel on Medium]]></title>
        <description><![CDATA[Stories by Sacha Arbonel on Medium]]></description>
        <link>https://medium.com/@sachaarbonel?source=rss-2c0d6b5ff54a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*QnjskaPGyy8HBW9KR0TIrw.jpeg</url>
            <title>Stories by Sacha Arbonel on Medium</title>
            <link>https://medium.com/@sachaarbonel?source=rss-2c0d6b5ff54a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:49:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sachaarbonel/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to build a Semantic Search Engine in Rust]]></title>
            <link>https://sachaarbonel.medium.com/how-to-build-a-semantic-search-engine-in-rust-e96e6378cfd9?source=rss-2c0d6b5ff54a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e96e6378cfd9</guid>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[recommendation-engine]]></category>
            <category><![CDATA[kd-tree]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[semantic-search]]></category>
            <dc:creator><![CDATA[Sacha Arbonel]]></dc:creator>
            <pubDate>Sat, 05 Nov 2022 21:59:53 GMT</pubDate>
            <atom:updated>2022-11-05T22:12:02.702Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/951/1*CpY8K_hAPXQ8jKM1KNGmMw.png" /><figcaption>“A Crab Searching for books in a book shelf using a magnifying glass”</figcaption></figure><h3>Introduction</h3><p>A <a href="https://en.wikipedia.org/wiki/Semantic_search">semantic search</a> engine is a type of <a href="https://en.wikipedia.org/wiki/Recommender_system">recommender system</a> that relies on the meaning of words to provide better search results. It is different from a traditional full text search engine, which relies on keyword matching to provide results.</p><p>A semantic search engine allows you to search for concepts, not just keywords. It understands meaning and the relationships between different concepts and can provide more relevant results based on those relationships.</p><p>In this article, we will discuss how to build a semantic search engine in <a href="https://www.rust-lang.org/">Rust</a>. We’ll explain what are embeddings, transformers, and nearest neighbor searches and how to perform them using KD-trees.</p><h3>What Are Embeddings?</h3><p><a href="https://en.wikipedia.org/wiki/Embedding">Embedding</a> is a term used in machine learning and statistics. Embeddings are mathematical representations of data that can be used for dimensionality reduction, similarity measurement, and many other tasks.</p><p>In <a href="https://en.wikipedia.org/wiki/Natural_language_processing">natural language processing</a> (NLP), text embedding is a mapping of a word or phrase into a vector of real numbers. The vector represents the word or phrase in a high-dimensional space.</p><p>The idea of word embeddings was first proposed in the early 2000s, but it wasn’t until 2013 that they became widely used with the release of the <a href="https://en.wikipedia.org/wiki/Word2vec">word2vec</a> algorithm and then <a href="https://en.wikipedia.org/wiki/BERT_(language_model)">BERT</a> in 2018.</p><h3>What Is a Transformer?</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FcmxqWSRJQfr6p9dIOd%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fbumblebeemovie-transformers-bumblebee-movie-cmxqWSRJQfr6p9dIOd&amp;image=https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2FcmxqWSRJQfr6p9dIOd%2Fgiphy.gif%3Fcid%3D790b7611a139c83eb3e3dc9de5c20a1b1c0b2cba2db18978%26rid%3Dgiphy.gif%26ct%3Dg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/edde6eaae47ad5358d195b7280a429b1/href">https://medium.com/media/edde6eaae47ad5358d195b7280a429b1/href</a></iframe><p>Embeddings are used in NLP tasks such as machine translation, question answering, and text classification. They are also used in recommender systems and <a href="https://en.wikipedia.org/wiki/Knowledge_graph">knowledge graphs</a>.</p><p>A <a href="https://en.wikipedia.org/wiki/Transformer_(machine_learning_model)">transformer</a> is a type of neural network that can be used for NLP tasks such as machine translation and question answering. The transformer was first proposed in 2017 in the paper Attention Is All You Need.</p><p>Transformers are different from traditional recurrent neural networks (RNNs) in that they do not require sequential data. This makes them well suited for tasks like machine translation, where the input and output are not necessarily in the same order.</p><p>Transformers are also more parallelizable than RNNs, which makes them faster to train.</p><p>Sentence Transformers is a state-of-the-art model for text and image embeddings.</p><h3>How to Use Sentence Transformers for Text Embeddings?</h3><p>The <a href="https://crates.io/crates/rust-bert">rust-bert</a> crate provides ready-to-use NLP pipelines and transformer-based models (BERT, DistilBERT, GPT2,…) in Rust. The crate also provides a Sentence Embeddings model.</p><p>In this article, we will use the rust-bert library to generate text embeddings. We will use the <a href="https://huggingface.co/sentence-transformers/all-MiniLM-L12-v2">all-MiniLM-L12-v2</a> model, which is a pre-trained model that uses the MiniLM <a href="https://arxiv.org/abs/2002.10957">architecture</a>.</p><p>For example, to get the text embedding for the sentence “this is an example sentence” and “each sentence is converted”:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc814bccd1af5d80ce364ed6739073fd/href">https://medium.com/media/dc814bccd1af5d80ce364ed6739073fd/href</a></iframe><h3>Nearest Neighbor Search</h3><p>A <a href="https://en.wikipedia.org/wiki/Nearest_neighbor_search">nearest neighbor search</a> is a method for finding the closest datapoint to a given query point. The query point can be any datapoint, not just a point in the dataset.</p><p>The goal of a nearest neighbor search is to find the datapoint that is most similar to the query point. Similarity can be measured using any distance metric, such as the Euclidean distance or the cosine similarity.</p><h3>What Is a KD-Tree?</h3><p>A <a href="https://en.wikipedia.org/wiki/K-d_tree">KD-tree</a> is a type of data structure that can be used for efficient nearest neighbor searches. A KD-tree is a <a href="https://en.wikipedia.org/wiki/Binary_tree">binary tree</a> where each node is divided into two children based on a splitting criterion.</p><p>The splitting criterion can be any function that maps a datapoint to a real number. The most common splitting criterion is the Euclidean distance.</p><p>KD-trees are very efficient for nearest neighbor searches because they reduce the search space by half at each level of the tree.</p><p>For example, if we want to find the datapoint that is nearest to the point (3.1, 0.9, 2.1), we can use a KD-tree to reduce the search space from the entire dataset to just the datapoints that are closest to (3.1, 0.9, 2.1).</p><p>In Rust, we can use the <a href="https://crates.io/crates/kd-tree">kd-tree</a> crate to build a KD-tree. We can use the build_by_ordered_float function to create a KD-tree from a list of points.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5b0d11176ba4403e6b1b878acd559b5/href">https://medium.com/media/b5b0d11176ba4403e6b1b878acd559b5/href</a></iframe><h3>Implementing a Semantic Search Engine</h3><p>In this section, we will discuss how to build a semantic search engine in Rust. We will use the Rust NLP crates rust-bert and kd-tree to perform similarity searches on books. In applications, it is a feature usually referred to as “Similar Books”.</p><p>Now we can write the code for our semantic search engine. We will start by loading our data stored in json, encode the embeddings and feed them to our kdtree.</p><h4>Prerequisites</h4><p>Having Rust installed</p><h4>Depedencies</h4><p>Let’s create our project called “semantic-search”</p><pre>cargo new semantic-search</pre><p>And add our dependencies we need</p><pre>cd semantic-search <br>cargo add serde --features derive <br>cargo add rust-bert <br>cargo add anyhow <br>cargo add kd-tree <br>cargo add typenum</pre><h4>Representing Our Books in Json</h4><p>Create a file called books.json to store our json data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc13307e029cd8382637c342f376fb88/href">https://medium.com/media/fc13307e029cd8382637c342f376fb88/href</a></iframe><h4>Deserializing Our Json Data Into A Struct</h4><p>Our program will load the json data into a struct. We can use the serde crate to deserialize the json data into our struct.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a758dba77b07214fec307ca69a36a60d/href">https://medium.com/media/a758dba77b07214fec307ca69a36a60d/href</a></iframe><p>We can now read the json from a file using the std::fs module and deserialize the json into our Book struct.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f5932f123b547167c24f79cfc025736/href">https://medium.com/media/1f5932f123b547167c24f79cfc025736/href</a></iframe><h4>Encoding Book Summaries Into Embeddings</h4><p>We can now use our model to encode each of our book summaries into embeddings. We will use the rust-bert crate to perform the encoding. We&#39;ll also need a convenient method to convert our book model into an Embedded book model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/481c2f4e5eabb0e494ff2dbac4e4d626/href">https://medium.com/media/481c2f4e5eabb0e494ff2dbac4e4d626/href</a></iframe><p>Finally, call encode for each book and add it to the embeddedbooks vector:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef9e9aab47aad700f5e1933c79507651/href">https://medium.com/media/ef9e9aab47aad700f5e1933c79507651/href</a></iframe><h4>Creating the KD-tree</h4><p>Before performing our search, first, we need to create a KD-tree from our embeddedbooks vector. We can do this using the sort_by function from the kd-tree crate.</p><p>We also need to implement the KdPoint trait for our EmbeddedBook struct. This trait allows us to use our EmbeddedBook struct with the kd-tree crate.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65456271e68bebe6ad84999476f01111/href">https://medium.com/media/65456271e68bebe6ad84999476f01111/href</a></iframe><p>Now we can use the sort_by function to create a KD-tree from our embeddedbooks vector.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f50015252d64288d844c2806e49aef3/href">https://medium.com/media/5f50015252d64288d844c2806e49aef3/href</a></iframe><h4>Performing the Nearest Neighbour Search</h4><p>Now that we have encoded our books into embeddings, we can use a nearest neighbor search to find the book that is most similar to a given query.</p><p>We need a convenient topic method on EmbeddedBook to create a dummy book with authors, title etc to for topic embedding.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5960e7c24c75c5da8395b59cbd1ca32/href">https://medium.com/media/b5960e7c24c75c5da8395b59cbd1ca32/href</a></iframe><p>We can use the kd-tree crate’s nearests function to perform the search. This function takes a query point and a number of results to return.</p><p>In this case, we will use the query “rich” and return 10 results.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19fbb82bfae8d8e364e2f28aa29c3d81/href">https://medium.com/media/19fbb82bfae8d8e364e2f28aa29c3d81/href</a></iframe><p>After running our program, this is what we are getting</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jLUVQSU46QfnPU9o" /></figure><h3>Conclusion</h3><p>In this article, we’ve discussed how to build a basic semantic search engine in Rust. We’ve explained what are embeddings, transformers, and nearest neighbor searches, and how to perform them using KD-trees.</p><p>We’ve also discussed how to use the rust-bert crate to generate text embeddings, and how to use the kd-tree crate to perform nearest neighbor searches.</p><p>You can find the source code in this <a href="https://github.com/sachaarbonel/semantic-search">repository</a>.</p><p><strong>Where to go from here? What can we improve?</strong></p><p>Well, keeping those embeddings in memory and running the model everytime we run the program is not a very scalable solution, in a real world scenario. Also this kd-tree crate doesn’t support removing or updating nodes in our tree. You could try this <a href="https://github.com/uzushino/rs-annoy">rs-annoy</a> project. Annoy is what is used at Spotify to store embeddings of songs. There is also this <a href="https://github.com/hora-search/hora">hora</a> completely written in Rust.</p><p>You could serialize the embedded books using the <a href="https://crates.io/crates/bincode">bincode</a> crate on the EmbeddedBook struct along with the <a href="https://crates.io/crates/serde-big-array">serde-big-array</a> create for the serialization on our embedding field. There is an example on how to do serialize structs into binary in <a href="https://rust-by-example-ext.com/serde/bincode.html">this Rust by example guide</a>, the struct just have to derive the Serialize trait from serde.</p><p>Or you could use the <a href="https://github.com/pgvector/pgvector">pgvector Postgres extension</a> and the <a href="https://crates.io/crates/pgvector">corresponding Rust crate</a> to respectively store them on postgres and query them using their builtin operator &lt;-&gt; for performing neighbour search.</p><p>Instead of our cli, we can also serve our recommendations to end users by wrapping our engine using a REST api with <a href="https://crates.io/crates/axum">axum</a> for example.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e96e6378cfd9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build an iMessage Clone with Stream’s Flutter Chat SDK]]></title>
            <link>https://sachaarbonel.medium.com/build-an-imessage-clone-with-streams-flutter-chat-sdk-7e84e28e8dec?source=rss-2c0d6b5ff54a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e84e28e8dec</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[messaging]]></category>
            <category><![CDATA[cross-platform]]></category>
            <dc:creator><![CDATA[Sacha Arbonel]]></dc:creator>
            <pubDate>Fri, 26 Feb 2021 17:57:43 GMT</pubDate>
            <atom:updated>2021-03-03T01:05:23.990Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*7OGNJsptB1MK4kVygS884g.png" /></figure><p>In this tutorial, we’ll build a functional clone of iMessage using <a href="https://pub.dev/packages/stream_chat_flutter">Stream Chat Flutter SDK</a>. Building a chat in your app at scale is not an easy task; but in this tutorial, you’ll get a chat experience up and running in roughly 20 minutes! You can follow this tutorial without a deep knowledge of Flutter, a basic understanding of the <a href="https://flutter.dev/docs/development/ui/layout#lay-out-multiple-widgets-vertically-and-horizontally">layout system</a>, Row and Column, we’ll do.</p><p>There are three ways to use our SDKs. Depending on how much abstraction you want.</p><p>Since we are trying to reproduce the look and feel of an app, in this tutorial we’ll use the way that give us the most degrees of liberty for customization: stream_chat_flutter_core. Also, it is also worth to note that to stay consistent with stream_chat_flutter package we&#39;ll keep the naming convention the same for widgets.</p><blockquote><em>Want to read this story later? </em><a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_read_later_text"><em>Save it in Journal.</em></a></blockquote><p>If you get lost during this tutorial, you can check:</p><p>- The <a href="https://github.com/GetStream/flutter-samples/tree/main/imessage">iMessage Clone</a> GitHub repo<br>- The <a href="https://getstream.io/chat/flutter/tutorial/">Stream Chat Flutter Tutorial</a></p><p>The result of our application will look similar to the following screenshots:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*Mxasq191End6E9jv" /></figure><p>Let’s get started! 🚀</p><h3>Prerequisites</h3><p>To proceed with this tutorial, you’ll need the following:<br>- <a href="https://flutter.dev/docs/get-started/install">Flutter SDK</a><br>- A Stream account. You can create one <a href="https://getstream.io/chat/">here</a> if you don’t have one.<br>- An Code editor such as <a href="https://code.visualstudio.com/download">Visual Studio Code</a></p><p>Install the sdk in pubspec.yaml:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c18af8c385776ec479240c3479321b8/href">https://medium.com/media/2c18af8c385776ec479240c3479321b8/href</a></iframe><p>Don’t forget to tap in your terminal flutter packages get to download the depedencies. But usually, if you use vscode they will be downloaded on save or when you run your code.</p><p>Let’s first build the static views then sprinkle 🪄 some Stream Flutter SDK magic here and there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/0*fq9bkDWiK41h3_8O" /></figure><p>We’ll first build the list view listing all conversations then the detailed view of those conversations.</p><h3>ChannelPreview</h3><p>Let’s start with showing up a preview of the conversations<br> In each item of the list we need to display three things:</p><ul><li>the contact that sent the message, including its avatar and name</li><li>a preview of the message. If there is a media, show a little emoji indicating if it is an image or a video (yes our product support all of that 😉)</li><li>the day of the week and the hour at which the message was received. If it was more than a week ago we’ll have to change the format for a nicer UX.</li></ul><p>Since iMessage is an iPhone app we’ll use <a href="https://flutter.dev/docs/development/ui/widgets/cupertino">Cupertino</a> Widgets, which are high-fidelity widgets for current iOS design system.</p><h3>Make it interactive</h3><p>To make our ChannelPreview widget interactive, we use <a href="https://api.flutter.dev/flutter/widgets/GestureDetector-class.html">GestureDetector</a>, among other things this widget is used for handling taps.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d5dd5f8b8c922a3cf47c9b3bde1a194/href">https://medium.com/media/3d5dd5f8b8c922a3cf47c9b3bde1a194/href</a></iframe><h3>Formating dates 📅</h3><p>To format the date we’ll use the <a href="https://pub.dev/packages/intl">intl</a> package is usually used to deal with internationalized/localized messages, date and number formatting and parsing, bi-directional text, and other internationalization issues. Let’s define these utility functions in utils.dart</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/361e4406ddd63534db4169857f6d966d/href">https://medium.com/media/361e4406ddd63534db4169857f6d966d/href</a></iframe><h3>ChannelListView 📺</h3><p>To build our listview we’ll use a <a href="https://flutter.dev/docs/development/ui/advanced/slivers">Sliver</a> widget. A sliver is just a portion of a scrollable area. Let’s call our widget ChannelListView because in Stream, since we have different <a href="https://getstream.io/chat/demos">use cases</a> such as livestream, customer support etc conversations happen in channels.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3667fc314a8ed7f59ed5b7771ec14292/href">https://medium.com/media/3667fc314a8ed7f59ed5b7771ec14292/href</a></iframe><h3>ChatLoader ⌛</h3><p>Let’s add a nice AppBar and wrap this in a widget that we call ChatLoader don&#39;t worry about channels parameters we&#39;ll explain later where does it come from.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59d7aae6590efd293cc6d788ef5e7dfe/href">https://medium.com/media/59d7aae6590efd293cc6d788ef5e7dfe/href</a></iframe><h3>MessagePage 📄</h3><p>Now that we have our listview displaying a preview of ours conversations. We need to navigate to individual items. Let’s call those items that hold each conversation MessagePage, since it displays messages. We&#39;ll need a navigation bar to display the contact with (avatar and name) whom we are having the discussion with and display the list of the messages. Let&#39;s call it MessageListView. Again, forget about messages parameter here. We&#39;ll see how we can add them with our Chat SDK.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f16bff82342782bad2fa80a8cbba83f/href">https://medium.com/media/1f16bff82342782bad2fa80a8cbba83f/href</a></iframe><h3>MessageListView</h3><p>In MessageListView we&#39;ll group messages by day like in the real app and change the color and display based on whether this is a message we sent or received. We&#39;ll need to &quot;draw&quot; a chat bubble. We&#39;ll also need an input to send those messages and attach medias to them.</p><h3>Group messages by date</h3><p>To group messages by day we use the function <a href="https://api.flutter.dev/flutter/package-collection_collection/groupBy.html">groupBy</a> to group the elements in values by the value returned by key, from the collection package.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c8e9380cdee95b38259ef70307010ff/href">https://medium.com/media/4c8e9380cdee95b38259ef70307010ff/href</a></iframe><h3>End Result</h3><p>Then it’s a matter of getting the date using entries[index].key and the list of messages entries[index].value and wrap this in a ListView builder as follow.</p><p>At the end, this is what the MessageListView widget will look like:</p><p>In our MessageWidget we want to check attachment type type, if it is an image or video, and display it accordingly</p><h3>MessageWidget</h3><p>In our `MessageWidget` we want to check attachment type type, if it is an image or video, and display it accordingly</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30bd7b92ae367fadfabbe255e0106449/href">https://medium.com/media/30bd7b92ae367fadfabbe255e0106449/href</a></iframe><h3>ChatBubble 💬</h3><p>To draw our bubble we use a <a href="https://api.flutter.dev/flutter/rendering/CustomPainter-class.html">CustomPainter</a>, which is a widget used to draw custom shapes and paths. The api surface is a bit like html canvas (if you are familiar with it). Let’s simply call this widget ChatBubble that takes into parameters the color and the alignment. We&#39;ll display the bubble differently according to alignment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/acc24f2f36fad5d4e0480f4636603e53/href">https://medium.com/media/acc24f2f36fad5d4e0480f4636603e53/href</a></iframe><h3>Message Input</h3><p>In our view now that we have displayed the messages we need to send a message. To do so, let’s call our widget MessageInput. We use the <a href="https://pub.dev/packages/image_picker">ImagePicker</a> plugin to take a photo from the gallery and <a href="https://getstream.io/chat/docs/flutter-dart/file_uploads/?language=dart">uploading</a> it along with our message.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ef9b955d274204e11999d057d16a17c/href">https://medium.com/media/7ef9b955d274204e11999d057d16a17c/href</a></iframe><h3>Spicing it up with Stream Chat SDK 🌶️</h3><p>Now that everything is setup. “How do we make an actual chat?” You may ask.</p><p>Well that’s simple, let’s initialize our SDK and runApp to run our top widget IMessage that we haven&#39;t defined yet, until now.</p><p>For this to work you’ll need an api key, and an user token that you can get in your <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwias6uz34XvAhVFiFkKHVzZAtIQFjAAegQIARAE&amp;url=https%3A%2F%2Fgetstream.io%2Fdashboard%2F&amp;usg=AOvVaw3-zbmrMWbdOoZxfJAaLxHf">dashboard</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/343917390605dcc102ac8628f1bb5c7d/href">https://medium.com/media/343917390605dcc102ac8628f1bb5c7d/href</a></iframe><h3>ChannelListCore</h3><p>Remember when we set up our <a href="https://getstream.io/blog/build-an-imessage-clone-with-streams-flutter-chat-sdk/#ChatLoader">ChatLoader</a> ? ChannelListView was taking the parameter channels but we did&#39;nt explained where does it come from. Now is the time to add the missing piece. Let me present you,<br> your two new best friends: <a href="https://pub.dev/documentation/stream_chat_flutter_core/latest/stream_chat_flutter_core/ChannelsBloc-class.html">ChannelsBloc</a> and <a href="https://pub.dev/documentation/stream_chat_flutter_core/latest/stream_chat_flutter_core/ChannelListCore-class.html">ChannelListCore</a>. ChannelListCore exposes builders to let you customize how to handle errors, loading progress, but most importantly it exposes List&lt;Channel&gt;. It also has options for filters, sorting, and pagination.</p><p>Our new ChatLoader we&#39;ll look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06986bff6cacd2cd47a7766a36eb17f8/href">https://medium.com/media/06986bff6cacd2cd47a7766a36eb17f8/href</a></iframe><h3>LazyLoadScrollView 🦥</h3><p><a href="https://pub.dev/documentation/stream_chat_flutter_core/latest/stream_chat_flutter_core/LazyLoadScrollView-class.html">LazyLoadScrollView</a>, is a wrapper around a Scrollable which triggers onEndOfPage/onStartOfPage when the Scrollable reaches to the start or end of the view extent. It exposes callbacks like onRefresh which comes handy in our case with controller.loadData() and controller.paginateData()</p><h3>MessageListCore</h3><p>Same patterns go for <a href="https://getstream.io/blog/build-an-imessage-clone-with-streams-flutter-chat-sdk/#MessagePage">MessagePage</a>, thanks to <a href="https://pub.dev/documentation/stream_chat_flutter_core/latest/stream_chat_flutter_core/MessageListCore-class.html">MessageListCore</a> you can have access to different builders, including the one exposing List&lt;Message&gt;:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6f054343cfa5aaf882796328153bf14/href">https://medium.com/media/c6f054343cfa5aaf882796328153bf14/href</a></iframe><h3>Congratulations! 👏</h3><p>This concludes part one of our tutorial on building a iMessage clone using Stream’s Flutter Chat SDK. I hope you found this tutorial useful, and I look forward to hearing your feedback.</p><p>In a next article — which will be published later — we will cover how to implement a feature such as Search to search through messages.</p><p>Happy coding!</p><p><em>Originally published at </em><a href="https://getstream.io/blog/build-an-imessage-clone-with-streams-flutter-chat-sdk/"><em>https://getstream.io</em></a><em>.</em></p><p>📝 Save this story in <a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_read_later_text">Journal</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e84e28e8dec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to call a Rust function from Dart using FFI]]></title>
            <link>https://medium.com/flutter-community/how-to-call-a-rust-function-from-dart-using-ffi-f48f3ea3af2c?source=rss-2c0d6b5ff54a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f48f3ea3af2c</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[rust]]></category>
            <dc:creator><![CDATA[Sacha Arbonel]]></dc:creator>
            <pubDate>Mon, 23 Sep 2019 16:48:31 GMT</pubDate>
            <atom:updated>2020-10-03T21:33:01.998Z</atom:updated>
            <content:encoded><![CDATA[<h3>Flutter hits version 1.9 with Dart 2.5 🎉</h3><p>What a moment to be alive guys ! More seriously … this a huge <a href="https://venturebeat.com/2019/09/10/google-launches-dart-2-5-with-intelligent-code-completion-flutter-1-9-with-ios-13-and-macos-catalina-support/">update</a>, Flutter for Web is no longer in technical preview (no weird imports anymore), and Dart has now support for FFI. Relax, it is not a boring article where I’ll detail every aspects of the changelog. After all it is my first article ever, so I’ll try to explain to you like you were five what is FFI and why do you need to show it some love.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*RKQYwHv8Zxs_O5k6v4XdKA.jpeg" /></figure><p>I didn’t want to make a dummy example neither a super complex one so to keep it simple, in this article / tutorial we will make a Dart Command Line Application that reads an audio file.</p><h3>But wait, what is FFI again ?</h3><p>A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another. When you need extra speed 🚀 or need to use a library of an other language, needless to say it becomes handy to have this kind of tool under our tool belt 👷‍.</p><h3>Why do I need FFI anyway ?</h3><p>Until today you were fine without it so why bother ? Because we are programmers and we want to optimize things. Right ?! No just kidding… FFI serves many purposes but the main strategy behind Dart language team focusing on this imho, is to smooth the path to Desktop development with Flutter, which is next stage for Flutter supremacy on the market (am I being dramatic here ?).</p><p>Well let’s say you are a lazy programmer like me, and you quickly need a library to play music/audio on your computer OR have THE music streaming Startup idea. Unfortunately no such thing exists in Flutter/ Dart because it is a relatively young ecosystem (the language is not that young actually). Without to say that Flutter for desktop still need some tooling to have the same experience that other supported platforms offer. Sooo, what are our options here?</p><ul><li>Write a library from scratch using native libraries written in C to play audio files? Nope, I’ll pass (as I said I’m a lazy programmer). Unfortunately I don’t know C and I sleep well at night (you’ll know why later 🦀) Furthermore, if you expect to share your work with the rest of the community (Yeah the Flutter community is awesome btw) you’ll have to write code that compiles on Mac, Linux, and Windows. Which means you’ll have to find a way to develop on all those platforms. Yeaah life sucks I know.</li><li>The smart option is to choose a language that is known for its portability on other platforms and its rich ecosystem of libraries. If you know these little guys,</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/0*1SqkmlybvRY_w-rZ" /><figcaption>to Go or not to Go</figcaption></figure><ul><li>I know what you are thinking , but not this time, it is a story for an other day. Why ? Mainly because <a href="https://golang.org/">Golang</a> doesn’t have a lot of good repositories to read an audio file in a cross platform fashion. Which language then ?</li></ul><p>Let me introduce you this little guy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*HQ5V8i39HfDuUqd7PGCh8Q.jpeg" /></figure><h3>Getting started with Rust and Dart FFI 🏎</h3><p>What I like the most about <a href="https://www.rust-lang.org/">Rust</a> is that it is relatively easy to learn coming from Golang, and there is no struggle at all to find free <a href="https://doc.rust-lang.org/book/">resources</a> to learn at your own pace.</p><p>When you search for rust audio cross platform in your favorite search engine you&#39;ll most likely end up on this forum <a href="https://users.rust-lang.org/t/the-cpal-and-rodio-libraries-audio-playback/2973">thread</a>.</p><p>Looks like a match 💓: <a href="https://github.com/RustAudio/rodio">339</a> ⭐ 116,656 <a href="https://crates.io/crates/rodio">dowloads</a>. It has support for Macos, Linux and Windows + all audio formats. Niiice! Let the fun begin.</p><p>⚠️️ If you want to follow along make sure you have installed Rust on your computer and (obviously) Flutter</p><p>Let’s give a name to our pet project. I called mine “rusty_ffi”. First we will scaffold our dart project with <a href="https://github.com/dart-lang/stagehand">stagehand</a>. In your terminal type:</p><pre>mkdir rusty_ffi <br>cd rusty_ffi <br>stagehand console-full <br>pub get</pre><p>Let’s create your (first ?) Rust project. In your terminal type:</p><pre>cargo new rusty_ffi</pre><p>It will scaffold a project for you like this :</p><pre>rusty_ffi <br>├── Cargo.toml <br>└── src <br>└── main.rs</pre><p>Let’s move the content of the rusty_ffi folder into the root folder :</p><pre>cp -r rusty_ffi/. .<br>rm -rf rusty_ffi</pre><p>You should end up with a folder that looks like this one :</p><pre>├── analysis_options.yaml<br>├── bin<br>│   └── main.dart<br>├── Cargo.toml<br>├── CHANGELOG.md<br>├── lib<br>│   └── rusty_ffi.dart<br>├── pubspec.lock<br>├── pubspec.yaml<br>├── README.md<br>├── src<br>│   └── main.rs<br>└── test<br>    └── rusty_ffi_test.dart</pre><p>You can run your “hello world” program shipped by default using cargo run. Yeah congrats if you made it to this point ! Now let&#39;s add our audio library to Cargo.toml :</p><pre>[package]<br>name = “rusty_ffi”<br>version = “0.1.0”<br>authors = [“Yourname &lt;<a href="/service/mailto:youremail@yourdomain.com">youremail@yourdomain.com</a>&gt;”]<br>edition = “2018”</pre><pre>[dependencies]<br>rodio = “0.9.0” #added rodio library</pre><p>Now that we have our audio library added as a depedency to our project, let’s play some audio shall we. According to the docs of Rodio, playing an audio file called beep.wav is as simple as this:</p><pre>use rodio;<br>use std::fs::File;<br>use std::io::BufReader;<br>use std::thread;<br>use std::time::Duration;</pre><pre>fn play_once(file_name : &amp;str){<br>    let device = rodio::default_output_device().unwrap(); // instantiate rodio with the default speaker<br>    let file = File::open(file_name).unwrap(); // open file named beep.wav<br>    rodio::play_once(&amp;device, BufReader::new(file)).unwrap(); // play audio<br>    thread::sleep(Duration::from_millis(1500)); // wait 1.5 s until stop playing<br>}<br>fn main() {<br>    play_once(&quot;beep.wav&quot;);<br>}</pre><p>Don’t worry it’s not a pocket call just your program running. Yeaaah !</p><h3>A little bit of refactoring</h3><p>We need to transform our program into a library by changing main.rs into lib.rs.</p><pre># ...<br># occulted<br>[lib]<br>name = &quot;play_once&quot;<br>crate-type = [&quot;cdylib&quot;] #dynamic library</pre><pre>[dependencies]<br>rodio = &quot;0.9.0&quot;</pre><p>Then export our C API :</p><pre>// ...<br>// occulted<br>use std::ffi::CStr;<br>use std::os::raw::c_char;</pre><pre>#[no_mangle]<br>pub extern &quot;C&quot; fn play_once(ptr: *const c_char) { //here is the trick<br>    let cstr = unsafe { CStr::from_ptr(ptr) }; //just two lines changed<br>    let device = rodio::default_output_device().unwrap(); <br>    let file = File::open(cstr.to_str().unwrap()).unwrap(); <br>    rodio::play_once(&amp;device, BufReader::new(file)).unwrap();<br>    thread::sleep(Duration::from_millis(1500)); <br>}</pre><p>Build your library with cargo build. Finally, in your bin/main.dart program add this :</p><pre>import &#39;dart:ffi&#39; as ffi;<br>import &#39;dart:ffi&#39;;<br>import &#39;package:ffi/ffi.dart&#39;;</pre><pre>typedef NativeRustPlayOnceFunction = ffi.Void Function(ffi.Pointer&lt;Utf8&gt;);<br>typedef NativePlayOnceFunction = void Function();</pre><pre>main() {<br>  ffi.DynamicLibrary dl =<br>      ffi.DynamicLibrary.open(&quot;target/debug/libplay_once.so&quot;);<br>  var play_once =<br>      dl.lookupFunction&lt;NativeRustPlayOnceFunction, NativeRustPlayOnceFunction&gt;(<br>          &quot;play_once&quot;);<br>  final Pointer&lt;Utf8&gt; song = Utf8.toUtf8(&quot;data/beep.wav&quot;).cast();<br>  play_once(song);<br>}</pre><h3>Conclusion 🏁</h3><p>Run your dart cli app dart bin/main.dart, Profit ! 💸</p><p>In this article you:</p><ul><li>learned low level programming, congrats mate.</li><li>learned how to call a Rust function from Dart</li><li>revolutionized the Music Software industry, yep true story.</li></ul><p>I hope you enjoyed my first article, if so drop (pun intended 🦀) me some claps 👏. You can find the <a href="https://github.com/Sach97/rusty_ffi.dart">source code</a> on my repository. Let me know in the comment section below if you have some difficulties or suggestions to help me improve the quality of my futures blog posts. Follow me and stay tuned for my next articles, I’m working on integrating Flutter Web + Rust via Web Assembly. Flutter for the win 🏆.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AuT0GXEd7j21RI6uXlxYTQ.png" /><figcaption><a href="https://www.twitter.com/FlutterComm">https://www.twitter.com/FlutterComm</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f48f3ea3af2c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/how-to-call-a-rust-function-from-dart-using-ffi-f48f3ea3af2c">How to call a Rust function from Dart using FFI</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>