<?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 Siri McClean on Medium]]></title>
        <description><![CDATA[Stories by Siri McClean on Medium]]></description>
        <link>https://medium.com/@siri.mcclean?source=rss-51e2de9f8ed5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*gI_p3oDN938aiif-</url>
            <title>Stories by Siri McClean on Medium</title>
            <link>https://medium.com/@siri.mcclean?source=rss-51e2de9f8ed5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:49:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@siri.mcclean/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[Using Socket.io with React & Redux]]></title>
            <link>https://medium.com/@siri.mcclean/using-socket-io-with-react-redux-2cc6cd2b6093?source=rss-51e2de9f8ed5------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cc6cd2b6093</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[socketio]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Siri McClean]]></dc:creator>
            <pubDate>Mon, 01 Oct 2018 11:52:33 GMT</pubDate>
            <atom:updated>2018-10-01T11:52:33.063Z</atom:updated>
            <content:encoded><![CDATA[<p>An Example of Using Socket.io with REACT &amp; REDUX</p><p>Hi everyone! I recently worked on a project that required the use of sockets. I found it fairly confusing to figure out how to incorporate it into my code and thought I would write a walk through to help any future socket users. Read on for more socket fun!</p><p>A bit of background on the project and why I needed to employ the use of sockets: I was working on doing an adaptation of the card game mafia. This is a multiplayer game where users are assigned different roles of mafia or innocent. The game consists of recurring rounds, alternating between a round of mafia ‘killing off’ innocents and a round where all players vote on who they think are mafia. In the end if the mafia manage to kill off all players, they win and vice versa, if the innocents are able to successfully vote off the mafia. For more about the game, look <a href="https://en.wikipedia.org/wiki/Mafia_(party_game)">here</a>.</p><p>The key to why the game required sockets lay in that what players see on the screen and what they were able to do in the game depending on 1) their assigned role: mafia vs innocent and 2) whether they were dead or alive. To keep track of these two things and make sure any changes were properly updated and transmitted to every other player is key to why sockets needed to be employed.</p><p>I used React for the components in my front-end and Redux to manage the state, which determined how my components were rendered. As the state changed to reflect the phase in the game, players assigned different roles would see or not see certain components, for example before voting, all players would see a status list of all players and whether they were dead and alive, during the round for mafia to kill off innocents only the mafia would see a page to choose non-mafia players, etc.</p><p>Establishing sockets allows information to be transmitted between the server and players. For example, when a player first starts and joins a game, they see a list of all the players currently in that game room. As new players join, I would like the list to immediately update on all player screens. Sockets are basically monitoring for any change in the state for different players and transmitting that information to the server, which can in turn transmit that change to each player.</p><p>So let’s say Hazel starts and joins a game. She sees a list with only her name as she is the only one currently in the game. Then Forest joins the same game, he sees a list with both his name and Hazel’s name. Without sockets, even though Forest joined the game, Hazel doesn’t see his name on the list. Adding sockets to listen in for new players would allow for an immediate change in Hazel’s screen, it would automatically update as soon as he joined without Hazel needing to refresh. The socket would listen for this event and transmit out the new information as soon as the event was triggered.</p><p>The first thing to do is npm install socket &amp; socket.io &amp; socket.io-client. Visit the socket.io <a href="https://socket.io/">website</a> for more information on installation.</p><p>Next I created a socket folder. Within this folder in a socket.js file, I housed the code for my server sockets. Next I created a socket.js file within my client folder for my client socket code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*LnUodVMy28qVmGc6c8lLow.png" /><figcaption>server socket code</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*wnGqI2Q3tUt3hsfLG615XA.png" /><figcaption>client socket code</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*DGbKsjLLC5hNr_Wn_RXrBQ.png" /><figcaption>socket code in React Thunk</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cc6cd2b6093" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>