<?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 Luke Walczak on Medium]]></title>
        <description><![CDATA[Stories by Luke Walczak on Medium]]></description>
        <link>https://medium.com/@_happiryu?source=rss-1b5f9152257c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*TF96LU-0vTRkKT3T.jpg</url>
            <title>Stories by Luke Walczak on Medium</title>
            <link>https://medium.com/@_happiryu?source=rss-1b5f9152257c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 16 Jun 2026 13:05:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@_happiryu/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[Simple chat app with React Native — Part II]]></title>
            <link>https://medium.com/call-stack/simple-chat-app-with-react-native-part-ii-e3d8fd9c6cd4?source=rss-1b5f9152257c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3d8fd9c6cd4</guid>
            <category><![CDATA[socketio]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[messenger]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Luke Walczak]]></dc:creator>
            <pubDate>Thu, 28 Dec 2017 14:32:29 GMT</pubDate>
            <atom:updated>2017-12-28T14:49:04.410Z</atom:updated>
            <content:encoded><![CDATA[<h4>Creating frontend for messaging app</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rznfG6vcueO7XlSitpoGQg.jpeg" /></figure><p>In the previous part we discussed the creation of backend for messaging app. This time I would like to describe how to connect frontend with our backend. Step by step will cover every endpoint by adjusting adequate action for them and finally, present how it works.</p><p>This is the second part of Simple chat app with React Native :</p><ul><li><a href="https://blog.callstack.io/simple-chat-app-with-react-native-part-i-34d6ea4c2535">Creation of backend for messaging app</a></li><li><strong>Fullstack — connecting backend with frontend</strong></li></ul><h4>What tools do we need?</h4><p>Once more we need to answer this question. Surely, a tool for the state management is our must have. Why? Conversations and messages are changing constantly, therefore a tool which will help us control the application state is necessary. My first choice was <a href="https://redux.js.org/">Redux</a>, which manages state through a single JavaScript object, known as a data store, where all of our application’s state is located. We simply have to install redux with the React bindings:</p><pre>yarn add redux react-redux</pre><p>Did you hear about <a href="https://github.com/FaridSafi/react-native-gifted-chat">Gifted Chat</a>? It’s pretty cool complete chat UI for React Native, which will make displaying messages so much easier. Let’s add it to dependencies:</p><pre>yarn add react-native-gifted-chat</pre><h4>Connecting to the Socket.IO server</h4><p>Socket.IO provides bi-directional communication in realtime and works perfectly for chat based applications. What is the biggest advantage of using it? We don’t have to involve polling the server for changes, because server immediately will get a message and push it to connected client.</p><p>The Socket.IO server we created recently is listening to the <em>‘connection’</em> event, which is automatically sent by the client. Our task is to send the <strong>user id</strong> as init message, so that server can identify the user the socket is connected to.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d72b702bc730a09248efa16501c521b2/href">https://medium.com/media/d72b702bc730a09248efa16501c521b2/href</a></iframe><p>Let’s also add a listener for the ‘<em>message’</em> event on the server which receives messages from the client and creates a message object to store in the database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc6e6becc0a26ea8c6264b80ed452b5d/href">https://medium.com/media/bc6e6becc0a26ea8c6264b80ed452b5d/href</a></iframe><h4>Actions, reducers and store … so Redux</h4><p>Firstly, we need to define <strong>createConversation</strong> action. According to official documentation <strong>actions</strong> are:</p><blockquote>Payloads of information that send data from your application to your store. They are the <strong>only</strong> source of information for the store. You send them to the store using store.dispatch().</blockquote><p>Let’s dive into code represents creating conversation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74e2cde8ff95aba64ed1f79362089910/href">https://medium.com/media/74e2cde8ff95aba64ed1f79362089910/href</a></iframe><p>The action is calling for a specific endpoint, in this case, it’s /conversation . The data that is sent is <strong>friendId</strong>, so in the conversation are kept two ids: conversation and friend.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HCpCHalsb8ojbb9SwYwtsA.png" /><figcaption>Conversation state</figcaption></figure><p>Because of redux-thunk middleware, we are able to dispatch action when we get the response. In a case when a response doesn’t include an error, <strong>createConversationSuccess()</strong> action is dispatched sequentially with navigating to conversation chat view.</p><p><strong>Reducer’s</strong> job is to specify how the application’s state changes in response. <br>When conversation creation is successful, the returned value is an array of conversations, which are spread and overwritten by action payload, so <strong>action.conversation</strong>. Simultaneously <strong>currentConversationId</strong> is set on <strong>action.conversation.Id</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/138f8a499989d7148a5c96a3f8cd6b70/href">https://medium.com/media/138f8a499989d7148a5c96a3f8cd6b70/href</a></iframe><p>Creating a message is much simpler. It’s typical action from action creator. What does it mean? Just that action creator is a function which returns an action.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1472605b30e27d37916b537e3c1b1cd7/href">https://medium.com/media/1472605b30e27d37916b537e3c1b1cd7/href</a></iframe><p>In our example function is called <strong>sendMessage </strong>and returns action type <em>‘SEND_MESSAGE’</em>. The function accepts two arguments, where the first- <strong>conversationId </strong>is needed to identify a destination for second — <strong>message</strong>.</p><p>We are following optimistic update approach likewise WhatsApp does, which means that a message is immediately appended to conversation before server responds. If it fails, we should display an error and give the user an opportunity to retry his request.</p><p>Similarly as before, state is changing in reducer and the result is message added to the exact conversation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8edec053e9bc718a43a56e91b255a0a/href">https://medium.com/media/d8edec053e9bc718a43a56e91b255a0a/href</a></iframe><p>At this stage, we are ready to load created conversations and sent the messages. The actions for them are very similar. The differences are:</p><ul><li>an endpoint for which action is dispatched</li><li>passed argument (<em>conversationId</em>) that is needed only in receiving messages action.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce65bcfb2df155a87b3b2b0c986250f3/href">https://medium.com/media/ce65bcfb2df155a87b3b2b0c986250f3/href</a></iframe><p>How should the state change in reducer? Obviously, it will return state with all messages for given id.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94b6a04ecfb8cd175879ad42903a8a21/href">https://medium.com/media/94b6a04ecfb8cd175879ad42903a8a21/href</a></iframe><p><strong>Store</strong> is a perfect place for holding the whole state tree of chat application. It’s worth to split reducing function into separate functions as well and then use combineReducers which is passed to createStore.</p><p>It would look like:</p><pre>const reducers = combineReducers({<br>  user,<br>  friends,  <br>  conversations,<br>  messages,<br>  error,<br>})</pre><pre>const store = createStore(reducers, applyMiddleware(thunk));</pre><h4>Time for a little chat</h4><p>Our logic is ready, so let’s create the containers for displaying conversations. Most likely we need to prepare two views:</p><ul><li>user account panel with friend list with whom we are messaging and option to add mate to chat,</li><li>chat window for texting a message.</li></ul><p>It’s time to tackle the main view called <strong>UserAccount</strong>, where have to connect states from reducers and redux action. Here’s how it will look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffff0ffb0927fcc31ea6efd1f9635553/href">https://medium.com/media/ffff0ffb0927fcc31ea6efd1f9635553/href</a></iframe><p>To receive user’s friends and all conversations will dispatch two actions inside lifecycle method <strong>componentDidMount()</strong>. This approach ensures us, that data won’t be loaded until after the initial render and is fetched <em>only</em> from the client.</p><pre>componentDidMount() {<br>  this.props.onLoadFriends();<br>  this.props.onLoadConversations();<br>}</pre><p>So what happens to the other two actions? Let me explain it, but first look at the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/993248409493db1293d32df1965e0cb5/href">https://medium.com/media/993248409493db1293d32df1965e0cb5/href</a></iframe><p><strong>FriendList</strong> is a styled <em>FlatList </em>which renders a sorted array of friends<em>, </em>where a single friend is an object with <strong>_id </strong>and <strong>fullName</strong>. Tapping on friend item calls the <strong>onSetCurrentConversationId() </strong>function and navigates to chat view when the conversation has been started or otherwise will create new conversation by firing <strong>onCreateConversation()</strong>.</p><p><strong>ConversationChat </strong>is a view for texting a message, so the most appropriate place to connect sending and loading messages. I think, you are already familiar with it, but just in case check the code snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e23571e420a6fff60da4247de962158/href">https://medium.com/media/8e23571e420a6fff60da4247de962158/href</a></iframe><p>Once again within <strong>componentDidMount() </strong>we are dispatching action- this time <strong>onLoadMessages()</strong>.</p><pre>componentDidMount() {<br>  this.props.onLoadMessages(<br>    this.props.navigation.state.params.conversation.id,<br>  );<br>}</pre><p>I hope that you are still following, because it’s the time for implementing <strong>onSendMessage() </strong>action! Let’s start it by creating a function wrapper inside, where we will be sending the <em>‘message’ </em>event to the server. At the same time, we can update redux state. Then close the function with firing the action.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a842033113f716c062a84c120e12a9bf/href">https://medium.com/media/a842033113f716c062a84c120e12a9bf/href</a></iframe><p>The last step is to use <strong>GiftedChat </strong>where we pass the following props:</p><ul><li>messages as an array of messages to display,</li><li>onSend as a callback when user is sending a message,</li><li>user, which is just user’s id.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/367fe6069bf5c445d1904a309fd528a3/href">https://medium.com/media/367fe6069bf5c445d1904a309fd528a3/href</a></iframe><p>How does it work?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Tt8f52kkaMB8B2hoVQxNMA.gif" /></figure><p>I hope you enjoyed this article 👏🏼 and creating simple chat app is easier for you now. Everything you read today can find inside an <a href="https://github.com/lukewalczak/friendChat">friendChat</a> app.</p><p>Luke.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3d8fd9c6cd4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/call-stack/simple-chat-app-with-react-native-part-ii-e3d8fd9c6cd4">Simple chat app with React Native — Part II</a> was originally published in <a href="https://medium.com/call-stack">Callstack Engineers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple chat app with React Native — part I]]></title>
            <link>https://medium.com/call-stack/simple-chat-app-with-react-native-part-i-34d6ea4c2535?source=rss-1b5f9152257c------2</link>
            <guid isPermaLink="false">https://medium.com/p/34d6ea4c2535</guid>
            <category><![CDATA[chat-apps]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[socketio]]></category>
            <category><![CDATA[hapijs]]></category>
            <dc:creator><![CDATA[Luke Walczak]]></dc:creator>
            <pubDate>Tue, 24 Oct 2017 13:12:43 GMT</pubDate>
            <atom:updated>2017-11-27T15:47:32.372Z</atom:updated>
            <content:encoded><![CDATA[<h4>Creating backend for messaging app</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D-ECWplwhnfvAc1dLQGr3w.png" /></figure><p>At the beginning of my journey with React Native, building a chat app was a challenge for me. I thought that application development would be complicated and time-consuming. I couldn’t be more wrong! I’ve started acquiring knowledge that I needed to make simple chat app, then created a repository and it just happened!</p><p>I want to share with you what I have learned over the course of past months in series that consists of the following topics:</p><ul><li><strong>Creation of backend for messaging app</strong></li><li>Fullstack — connecting backend with frontend (December)</li></ul><h4>What tools do you need?</h4><p>First of all, we need a server. I have decided to use Hapi.js. Now, if you haven’t heard of it before, it’s a declarative configuration-centric framework. What does it mean? Unlike Express.js, which relies heavily on the concept of middlewares, Hapi.js gives you an opportunity to pass the configuration object that describes your route hierarchy.</p><p>Here’s how a typical setup would look like:</p><pre>const Hapi = require(&#39;hapi&#39;);</pre><pre>const server = new Hapi.Server();<br>server.connection({ port: 3000, host: &#39;localhost&#39; });</pre><pre>// route configuration as an object<br>const routes = [<br>  {<br>    method: &#39;GET&#39;,<br>    path: &#39;/&#39;,<br>    handler: function (request, reply) {<br>      reply(&#39;Hello, world!&#39;);<br>    },<br>  },<br>  {<br>    method: &#39;GET&#39;,<br>    path: &#39;/{name}&#39;,<br>    handler: function (request, reply) {<br>      reply(`Hello, ${request.params.name}`);<br>    },<br>  }<br>];</pre><pre>server.route(routes);</pre><pre>server.start((err) =&gt; {<br>  if (err) {<br>    throw err;<br>  }<br>  console.log(`Server running at: ${server.info.uri}`);<br>});</pre><p>What’s great about this approach is the ability to describe the endpoints your application supports and an opportunity to reuse it across your different projects.</p><p>Let’s start by adding <em>Hapi.js</em> to our dependencies:</p><pre>yarn add hapi</pre><blockquote>NOTE: You can use npm instead<em>.</em></blockquote><p>Next, we need a <strong>database</strong> to store our users and their conversations. I chose <a href="https://www.mongodb.com/">MongoDB</a>. Thanks to being NoSQL, it’s structure is similar to JSON objects, which makes it easy to navigate through the data. To define the schema for documents, we will use o<em>bject modeling tool </em>called <a href="http://mongoosejs.com/">Mongoose</a>.</p><p>It’s necessary to install <em>Mongo:</em></p><pre>yarn add mongodb<br>yarn add mongoose</pre><p>And finally, we will need <a href="https://socket.io/">Socket.IO</a>, which is a library that helps you to work with WebSockets. We will use it to implement real-time communication with our server, including receiving and ending data to connected sockets.</p><pre>yarn add socket.io</pre><h4>Setting up</h4><p>Let’s create a server based on Hapi.js. It allows us to handle http protocol and Socket.IO. Next step is to listen to <em>‘connection’ </em><strong>event</strong>, which is automatically sending by the client. Then on ‘<em>init’ </em>assign<em> </em>to socket sending user’s id from sockets object. Now, we just listen to <em>‘message’ </em>event that is sent from the client when the user sends a message. The message is directly transferred between two defined sockets. Simultaneously, when the app is closed, the <em>‘disconnect’ </em>event is fired.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44356584a5099cbd2d10188d6de66a7a/href">https://medium.com/media/44356584a5099cbd2d10188d6de66a7a/href</a></iframe><p>Every endpoint needs its own <strong>route with a specific handler</strong>, so we need to define it. The route could be something like:</p><pre>server.route({<br>    method: &#39;POST&#39;,<br>    path: &#39;/conversations&#39;,<br>    handler: handlers.createConversation,<br>    config: {<br>      auth: &#39;jwt&#39;,<br>      validate: {<br>        payload: {<br>          friendId: Joi.string().required(),<br>        },<br>      },<br>    },<br>  });</pre><h4>Conversation or message?</h4><p>At this stage, we need to catch the difference between conversation and message. <strong>Conversation</strong> is a container for messages (<a href="http://mongoosejs.com/docs/populate.html"><em>has a reference to the message model</em></a>) being sent between two users.</p><pre>{<br>  userOneId: String,<br>  userTwoId: String,<br>  messages: [{<br>    type: mongoose.Schema.Types.ObjectId,<br>    ref: &#39;Message&#39;,<br>  }],<br>}</pre><p><strong>Message</strong> model object should be similar to the one below:</p><pre>{<br>  text: String,<br>  createdAt: {<br>    type: Date,<br>    default: Date.now,<br>  },<br>  userId: Object,<br>}</pre><h4>Create a converations as a first handler</h4><p>I’m sure you have noticed that first handler has to be responsible for <strong>creating conversation</strong>. Let’s dive into it. First, you need to make sure that there has been no conversation between the two <strong>users</strong> yet. Creating a conversation includes adding new conversation object to the database for two messaging users. It looks easy! But is it really? Take a look at more handlers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/194f7ee7ea157673fc13a7b95c3026b9/href">https://medium.com/media/194f7ee7ea157673fc13a7b95c3026b9/href</a></iframe><h4>… and more handlers</h4><p>The container is ready for an <strong>initial message</strong>. To create it, we need to find a proper conversation by an <strong>id</strong>, which is sent from a <strong>client</strong> in message property. Then, we are able to create a textMessage, save it and push to the messages array as a part of a conversation model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50bb8d156fa24a471b1d8894df97a202/href">https://medium.com/media/50bb8d156fa24a471b1d8894df97a202/href</a></iframe><p>What should we do next? Imagine a case when your user starts a conversation, sends a new message and closes the app. Suddenly, he realizes he forgot to mention something important, so he re-opens the app… and gets confused: there is no conversation and no messages. Don’t worry, they’re not lost — they still exist in the database, waiting to be<strong> loaded</strong>. Naturally, we expect only a conversation with the chosen friend to load, so we need to distinguish two ids and reply the proper one extended with conversation id.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83c73b851a1f204040ee9e72919e6a3f/href">https://medium.com/media/83c73b851a1f204040ee9e72919e6a3f/href</a></iframe><p>Closing the app’s backend, we need to implement the last handler related to <strong>loading messages</strong> for a given conversation id.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/400c4e13abdacbca5e581899dc59dbb8/href">https://medium.com/media/400c4e13abdacbca5e581899dc59dbb8/href</a></iframe><p>Hooray, we’re done and our backend is set! 🎉</p><p>If you’re interested in how to connect our work with frontend — make sure to follow this series, because there’s another article coming up in December. Everything you read today, you can check out in my <a href="https://github.com/lukewalczak/friendChat/tree/backend">repo</a>. Enjoy!</p><p><em>May the force be with you…</em></p><p>Luke.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34d6ea4c2535" width="1" height="1" alt=""><hr><p><a href="https://medium.com/call-stack/simple-chat-app-with-react-native-part-i-34d6ea4c2535">Simple chat app with React Native — part I</a> was originally published in <a href="https://medium.com/call-stack">Callstack Engineers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>