<?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 Dan Kaufhold on Medium]]></title>
        <description><![CDATA[Stories by Dan Kaufhold on Medium]]></description>
        <link>https://medium.com/@d___a__n_?source=rss-a920801cea9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Jx6ma7PEjI4isayRcx6pwA.jpeg</url>
            <title>Stories by Dan Kaufhold on Medium</title>
            <link>https://medium.com/@d___a__n_?source=rss-a920801cea9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 04:06:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@d___a__n_/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[A Simple Chat App With React, Node and WebSocket]]></title>
            <link>https://medium.com/bitlab-studio/a-simple-chat-app-with-react-node-and-websocket-35d3c9835807?source=rss-a920801cea9------2</link>
            <guid isPermaLink="false">https://medium.com/p/35d3c9835807</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[websocket]]></category>
            <dc:creator><![CDATA[Dan Kaufhold]]></dc:creator>
            <pubDate>Sun, 16 Sep 2018 08:16:34 GMT</pubDate>
            <atom:updated>2020-10-21T11:55:46.763Z</atom:updated>
            <content:encoded><![CDATA[<p>In this quick guide I will be showing you the minimal code required to get a chat room up and running using React, Node and WebSocket.</p><p>It is a rudimentary summary of what I learned during a project for <a href="https://bitlabstudio.com/">Bitlab Studio</a>.</p><p>From there you can play around and explore and add own ideas and features.</p><p>In the end you will have an app that looks something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*u-qal413IzAMkIZMhWqYHg.gif" /></figure><p>You see a React app running in two browser windows that post their messages to a node app in the background, which broadcasts all messages back to all connected apps.</p><p>So let’s see how this is done!</p><h4>Step 1: Prepare The Backend</h4><p>Create a simple WebSocket server that broadcasts all incoming messages to everyone that’s connected. So in your project’s root directory run the following commands to create a separate backend directory and install ws :</p><pre>mkdir backend<br>cd backend<br>yarn add ws</pre><p>Then we will also need the actualserver.js file. Which is merely the following:</p><pre><strong>const </strong>WebSocket = require(&#39;ws&#39;);<br><br><strong>const </strong>wss = <strong>new </strong>WebSocket.Server({ port: 3030 });<br><br>wss.on(&#39;connection&#39;, <strong>function </strong>connection(ws) {<br>  ws.on(&#39;message&#39;, <strong>function </strong>incoming(data) {<br>    wss.clients.forEach(<strong>function </strong>each(client) {<br>      <strong>if </strong>(client !== ws &amp;&amp; client.readyState === WebSocket.OPEN) {<br>        client.send(data);<br>      }<br>    });<br>  });<br>});</pre><p>On each incoming message it sends it back to all connected clients.</p><h4>Step 2: Add The Frontend</h4><p>Optional - if you haven’t already: Install CRAyarn global add create-react-app</p><p>Then back in your project root you want to create a new react app:</p><pre>create-react-app frontend<br>cd frontend<br>yarn add prop-types</pre><p>You now have a frontend/ and a backend/ directory.</p><p>Create ChatMessage.js component in the new frontend/ directory. This determines how each single chat message will look like:</p><pre><strong>import </strong>React <strong>from </strong>&#39;react&#39;<br><br><strong>export default </strong>({ name, message }) =&gt;<br>  &lt;p&gt;<br>    &lt;strong&gt;{name}&lt;/strong&gt; &lt;em&gt;{message}&lt;/em&gt;<br>  &lt;/p&gt;</pre><p>It will output the name of the user in bold and then the message.</p><p>Create a ChatInput.js component. We will display it on top of the chat to enter new messages:</p><pre><strong>import </strong>React, { Component } <strong>from </strong>&#39;react&#39;<br><strong>import </strong>PropTypes <strong>from </strong>&#39;prop-types&#39;<br><br><strong>class </strong>ChatInput <strong>extends </strong>Component {<br>  <strong>static </strong><em>propTypes </em>= {<br>    onSubmitMessage: PropTypes.func.isRequired,<br>  }<br>  state = {<br>    message: &#39;&#39;,<br>  }<br><br>  render() {<br>    <strong>return </strong>(<br>      &lt;form<br>        action=&quot;.&quot;<br>        onSubmit={e =&gt; {<br>          e.preventDefault()<br>          <strong>this</strong>.props.onSubmitMessage(<strong>this</strong>.state.message)<br>          <strong>this</strong>.setState({ message: &#39;&#39; })<br>        }}<br>      &gt;<br>        &lt;input<br>          type=&quot;text&quot;<br>          placeholder={&#39;Enter message...&#39;}<br>          value={<strong>this</strong>.state.message}<br>          onChange={e =&gt; <strong>this</strong>.setState({ message: e.target.value })}<br>        /&gt;<br>        &lt;input type=&quot;submit&quot; value={&#39;Send&#39;} /&gt;<br>      &lt;/form&gt;<br>    )<br>  }<br>}<br><br><strong>export default </strong>ChatInput</pre><p>This component receives one prop onSubmitMessage from the Chat component we’re about to create, which is called when the form is submitted. Additionally when the form is submitted, the message input is cleared again.</p><p>Now create the Chat.js component, which will be the center of our chat logic. It holds our state, manage the connection and also send and receive the messages:</p><pre><strong>import </strong>React, { Component } <strong>from </strong>&#39;react&#39;<br><strong>import </strong>ChatInput <strong>from </strong>&#39;./ChatInput&#39;<br><strong>import </strong>ChatMessage <strong>from </strong>&#39;./ChatMessage&#39;<br><br><strong>const </strong>URL = &#39;ws://localhost:3030&#39;<br><br><strong>class </strong>Chat <strong>extends </strong>Component {<br>  state = {<br>    name: &#39;Bob&#39;,<br>    messages: [],<br>  }<br><br>  ws = <strong>new </strong>WebSocket(URL)<br><br>  componentDidMount() {<br>    <strong>this</strong>.ws.onopen = () =&gt; {<br>      // on connecting, do nothing but log it to the console<br>      console.log(&#39;connected&#39;)<br>    }<br><br>    <strong>this</strong>.ws.onmessage = evt =&gt; {<br>      // on receiving a message, add it to the list of messages<br>      <strong>const </strong>message = JSON.parse(evt.data)<br>      <strong>this</strong>.addMessage(message)<br>    }<br><br>    <strong>this</strong>.ws.onclose = () =&gt; {<br>      console.log(&#39;disconnected&#39;)<br>      // automatically try to reconnect on connection loss<br>      <strong>this</strong>.setState({<br>        ws: <strong>new </strong>WebSocket(URL),<br>      })<br>    }<br>  }<br><br>  addMessage = message =&gt;<br>    <strong>this</strong>.setState(state =&gt; ({ messages: [message, ...state.messages] }))<br><br>  submitMessage = messageString =&gt; {<br>    // on submitting the ChatInput form, send the message, add it to the list and reset the input<br>    <strong>const </strong>message = { name: <strong>this</strong>.state.name, message: messageString }<br>    <strong>this</strong>.ws.send(JSON.stringify(message))<br>    <strong>this</strong>.addMessage(message)<br>  }<br><br>  render() {<br>    <strong>return </strong>(<br>      &lt;div&gt;<br>        &lt;label htmlFor=&quot;name&quot;&gt;<br>          Name:&amp;nbsp;<br>          &lt;input<br>            type=&quot;text&quot;<br>            id={&#39;name&#39;}<br>            placeholder={&#39;Enter your name...&#39;}<br>            value={<strong>this</strong>.state.name}<br>            onChange={e =&gt; <strong>this</strong>.setState({ name: e.target.value })}<br>          /&gt;<br>        &lt;/label&gt;<br>        &lt;ChatInput<br>          ws={<strong>this</strong>.ws}<br>          onSubmitMessage={messageString =&gt; <strong>this</strong>.submitMessage(messageString)}<br>        /&gt;<br>        {<strong>this</strong>.state.messages.map((message, index) =&gt;<br>          &lt;ChatMessage<br>            key={index}<br>            message={message.message}<br>            name={message.name}<br>          /&gt;,<br>        )}<br>      &lt;/div&gt;<br>    )<br>  }<br>}<br><br><strong>export default </strong>Chat</pre><p>Take a closer look at the Chat component if you haven’t already. It creates a new WebSocket instance and adds handlers for messages as well as opening and closing connections. It passes the onSubmitMessage prop to our ChatInput which actually sends the message to the node backend. In addition it displays a name input, which we also send to the backend to identify the users.</p><p>Update your App.js to include the Chat component, so it actually renders on screen.</p><pre><strong>import </strong>React, { Component } <strong>from </strong>&#39;react&#39;<br><strong>import </strong>logo <strong>from </strong>&#39;./logo.svg&#39;<br><strong>import </strong>&#39;./App.css&#39;<br><strong>import </strong>Chat <strong>from </strong>&#39;./Chat&#39;<br><br><strong>class </strong>App <strong>extends </strong>Component {<br>  render() {<br>    <strong>return </strong>(<br>      &lt;div className=&quot;App&quot;&gt;<br>        &lt;header className=&quot;App-header&quot;&gt;<br>          &lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;<br>          &lt;h1 className=&quot;App-title&quot;&gt;Welcome to React&lt;/h1&gt;<br>        &lt;/header&gt;<br>        &lt;Chat /&gt;<br>      &lt;/div&gt;<br>    )<br>  }<br>}<br><br><strong>export default </strong>App</pre><h4>Step 3: Try It Out!</h4><p>Start your servers by running yarn start in the frontend/ directory and node server.js in the backend/ directory and point your browser (or multiple browser windows) to localhost:3000</p><p>Congrats, you’ve created your first chat room 🎉</p><h4>Step 4: Clone And Extend</h4><p>If you want to dive into the full code example you can clone it here and play around with it: <a href="https://github.com/bitlabstudio/blogpost-react-websocket-chat">https://github.com/bitlabstudio/blogpost-react-websocket-chat</a></p><p>Want to know more about Bitlab Studio? Visit <a href="https://bitlabstudio.com/">bitlabstudio.com</a>!</p><p>Send me examples of what you’ve built on top of my code! I might feature links to them here, if you want :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35d3c9835807" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bitlab-studio/a-simple-chat-app-with-react-node-and-websocket-35d3c9835807">A Simple Chat App With React, Node and WebSocket</a> was originally published in <a href="https://medium.com/bitlab-studio">Bitlab Studio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ultra Short Guide to Using Amazon S3 with Django]]></title>
            <link>https://medium.com/bitlab-studio/ultra-short-guide-to-django-and-amazon-s3-2c5aae805ce4?source=rss-a920801cea9------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c5aae805ce4</guid>
            <category><![CDATA[how-to]]></category>
            <category><![CDATA[amazon-s3]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[s3]]></category>
            <dc:creator><![CDATA[Dan Kaufhold]]></dc:creator>
            <pubDate>Wed, 23 Nov 2016 09:46:57 GMT</pubDate>
            <atom:updated>2019-05-08T06:48:56.590Z</atom:updated>
            <content:encoded><![CDATA[<p>Django and S3 have been a staple of <a href="https://bitlabstudio.com/">Bitlab Studio</a>’s stack for a long time. Here I want to show you how to put those two together.</p><p>Since you probably searched for this specifically on Google, I will assume, that you’re familiar with Django and already know, what S3 is and want to get right into the action. Well here we go!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*jWrMXHbEUyOMcHajh3NV7Q.png" /></figure><h3>Dependencies</h3><p>First you need to add the latest versions of <a href="https://django-storages.readthedocs.io/en/latest/">django-storages</a> and <a href="https://boto3.readthedocs.io/en/latest/">boto3</a> to your requirements (might need to bump up the versions in the future):</p><pre>django-storages==1.5.1<br>boto3==1.4.1</pre><h3>AWS Setup</h3><p>First you need to <a href="https://aws.amazon.com/">create an AWS account</a>.</p><h4>Option 1 — using user credentials</h4><p>You will need to get or <a href="http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html">create your user’s security credentials</a> from <a href="https://console.aws.amazon.com/iam/home">AWS IAM</a> (Identity and Access Management):</p><ul><li>AWS Access Key ID</li><li>AWS Secret Access Key ID</li></ul><p>Then you create a new S3 bucket from the <a href="https://console.aws.amazon.com/s3/home">AWS S3 console</a> in the availability zone you prefer.</p><p>Note, that you should never ever use your root account credentials for this!<br>For extra information consult the <a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html">IAM best practices</a>.</p><h4>Option 2 — using service role [recommended]</h4><p>For this you will need to add a service role to the EC2 instance while launching the instance. It’s on step 3 of the launch wizard.</p><p>Name the role something like myproject-instance-role, select Amazon EC2 under AWS Service Roles and finally give it the AmazonS3FullAccess permission.</p><p>When using a role, you need to set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY settings to None.<br>Boto will then automatically assume the instance role when accessing your bucket.</p><h3>Settings</h3><pre># add the credentials from IAM and bucket name<br>AWS_STORAGE_BUCKET_NAME = &#39;mybucketname&#39;  # or None if using service role<br>AWS_ACCESS_KEY_ID = &#39;MYACCESSKEYID&#39;  # or None if using service role<br>AWS_SECRET_ACCESS_KEY = &#39;mysecretaccesskey12345&#39;</pre><pre># if False it will create unique file names for every uploaded file<br>AWS_S3_FILE_OVERWRITE = False</pre><pre># the url, that your media and static files will be available at<br>AWS_S3_CUSTOM_DOMAIN = &#39;%s.s3.amazonaws.com&#39; % AWS_STORAGE_BUCKET_NAME</pre><pre># the sub-directories of media and static files<br>STATICFILES_LOCATION = &#39;static&#39;<br>MEDIAFILES_LOCATION = &#39;media&#39;</pre><pre># a custom storage file, so we can easily put static and media in one bucket<br>STATICFILES_STORAGE = &#39;myproject.custom_storages.StaticStorage&#39;<br>DEFAULT_FILE_STORAGE = &#39;myproject.custom_storages.MediaStorage&#39;</pre><pre># the regular Django file settings but with the custom S3 URLs<br>STATIC_URL = &quot;https://%s/%s/&quot; % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)<br>MEDIA_URL = &quot;https://%s/%s/&quot; % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)</pre><h3>Custom storage</h3><p>Create a file called custom_storages.py where you specified in the settings above (in myproject) and add the following code:</p><pre>from django.conf import settings<br>from storages.backends.s3boto3 import S3Boto3Storage</pre><pre>class StaticStorage(S3Boto3Storage):<br>    location = settings.STATICFILES_LOCATION</pre><pre>class MediaStorage(S3Boto3Storage):<br>    location = settings.MEDIAFILES_LOCATION</pre><h3>That’s it</h3><p>Now you can run python manage.py collectstatic and it will copy the static files into the static sub-directory of your S3 bucket.</p><p>File uploads will land in your media sub-directory.</p><h3>[OPTIONAL] Copy existing media files</h3><p>Install AWS CLI globally on your server:</p><pre>$ sudo apt-get install awscli<br>$ aws configure</pre><pre># enter credentials</pre><p>Copy the files from an existing media directory</p><pre>$ aws s3 cp /path/to/my/media s3://mybucketname/media --recursive --acl public-read-write</pre><h3>The end</h3><p>Thanks for checking out this post. Please make sure to like and share it, if it was of any help for you. That would help us a lot in return.</p><p>Please leave a comment, if this somehow doesn’t work for you and we’ll try our best to keep this post updated.</p><p>Want to know more about Bitlab Studio? Visit <a href="https://bitlabstudio.com/">bitlabstudio.com</a>!</p><p>Cheers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c5aae805ce4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bitlab-studio/ultra-short-guide-to-django-and-amazon-s3-2c5aae805ce4">Ultra Short Guide to Using Amazon S3 with Django</a> was originally published in <a href="https://medium.com/bitlab-studio">Bitlab Studio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>