<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Carnegie Hall</title>
    <description>The latest articles on DEV Community by Carnegie Hall (@alegendcodes).</description>
    <link>https://dev.to/alegendcodes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1928364%2Fd2e9d4e2-ff84-4f5e-b658-502a7219175c.png</url>
      <title>DEV Community: Carnegie Hall</title>
      <link>https://dev.to/alegendcodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alegendcodes"/>
    <language>en</language>
    <item>
      <title>Chatting with Websockets 101:</title>
      <dc:creator>Carnegie Hall</dc:creator>
      <pubDate>Fri, 03 Jan 2025 16:18:25 +0000</pubDate>
      <link>https://dev.to/alegendcodes/chatting-with-websockets-101-1767</link>
      <guid>https://dev.to/alegendcodes/chatting-with-websockets-101-1767</guid>
      <description>&lt;p&gt;Websockets are a very interesting new type of technology, they allow a multitude of different things for developers to be able to do with sites and apps such as: giving clients the ability to talk to one another in real time, showing the live score of a gaming match, and even sending traffic updates. The core feature of what makes Websockets so great is that it is used to broadcast information via a client and a server. the server often times recieves information and relays said information over to the client(s) for them to be able to view. If you've ever watched a live stream from tiktok you've witnessed websockets in action. Websockets uses what is called Full duplex bi directional Communication between the client and server which really just means the client and server speak to each other in real time without having to initiate a new request. I like to relate it to a page that is justs continous with it's chat feature, none of the old datat is deleted, and a new client can come in and join and create new data and see the old data being displayed. Here I'm going to display some code using SocketIO, Flask, Javascript &amp;amp; Python. &lt;/p&gt;

&lt;p&gt;To set up the SocketIO server we first want to follow these steps. &lt;/p&gt;

&lt;p&gt;after importing flask, flask socketio and flask cors, I'm creating a flask instance with app = Flask(&lt;strong&gt;name&lt;/strong&gt;) &lt;/p&gt;

&lt;p&gt;app = Flask(&lt;strong&gt;name&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;after which I'm adding a secret key for my Flask application this will help secure sessions and sign cookies for future use. &lt;/p&gt;

&lt;p&gt;app.config['SECRET_KEY'] = "secret!"&lt;/p&gt;

&lt;p&gt;Adding CORS (Cross Origin Resource Sharing) allows my routes to be accessed from any origin. &lt;/p&gt;

&lt;p&gt;CORS(app, resources={r"/&lt;em&gt;":{"origins":"&lt;/em&gt;"}})&lt;/p&gt;

&lt;p&gt;Here I'm initializing Flask-SocketIO to go and enable the bi directional communication we discussed before, the inner portion (app, cors_allowed_origins="*") is allowing connections from any origin much like how the previous CORS does. &lt;/p&gt;

&lt;p&gt;socketio = SocketIO(app, cors_allowed_origins="*")&lt;/p&gt;

&lt;p&gt;Our next step is to define a route in our flask application that will handle our HTTP request sent to our URL path. &lt;/p&gt;

&lt;p&gt;@app.route('/http-call')&lt;br&gt;
def http_call():&lt;br&gt;
    data = {'data':'text fetched via an HTTP Call to server on render'}&lt;br&gt;
    return jsonify(data)&lt;/p&gt;

&lt;p&gt;When a client sends a request to our localhost3000/http-call we'll invoke this above function. &lt;/p&gt;

&lt;p&gt;@socketio.on('connect')&lt;br&gt;
def connected():&lt;br&gt;
    print(request.sid)&lt;br&gt;
    print("Client is connected")&lt;/p&gt;

&lt;p&gt;Here we created a function that listens for the connect event in our Flask-SocketIO app. Once a new client connects with a websocket to our server our above function is executed and prints a response informing our chat feed that someone new has connected.&lt;/p&gt;

&lt;p&gt;@socketio.on('disconnect')&lt;br&gt;
def disconnected():&lt;br&gt;
    print("User disconnected")&lt;br&gt;
    emit("disconnect", f"user {request.sid} has been disconnected", broadcast=True)&lt;/p&gt;

&lt;p&gt;our second function here is our disconnect function that informs our chat that the user has disconnected once their websocket is no longer active. &lt;/p&gt;

&lt;p&gt;@socketio.on('data')&lt;br&gt;
def handle_message(data):&lt;br&gt;
    print("Data from the front end: ",str(data))&lt;br&gt;
    emit("data",{ &lt;br&gt;
        'data': data, 'id':request.sid&lt;br&gt;
    }, broadcast=True)&lt;/p&gt;

&lt;p&gt;our last function sends information to our client side. Here we are emitting data back to all our connected clients including the person who sent their message. Here we pass in broadcast=True as the unique argument to ensure the message is sent to all of our connected clients. our request.sid is sent with our event. I do want to note that the request.sid works almost like a social security number as it serves as the unique ID of the client that is sending the message. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Formik &amp; Yup simplified:</title>
      <dc:creator>Carnegie Hall</dc:creator>
      <pubDate>Fri, 15 Nov 2024 04:24:01 +0000</pubDate>
      <link>https://dev.to/alegendcodes/formik-yup-simplified-ndm</link>
      <guid>https://dev.to/alegendcodes/formik-yup-simplified-ndm</guid>
      <description>&lt;p&gt;When learning how to bridge the gap between Backend and Frontend applications it can feel like a lot, especially when it comes to whether you want to use constraints or validations. Luckily React gives you an option to import Formik and Yup. Doing this helps us not only simplify validation handling, but helps with our scalability and maintainability. Both Formik and Yup are libraries, however both vary in features and functionality. Formik's library assists in the creation and management of forms in React applications. Yup on the other hand is a Javascript schema validation that defines validation schemas for our form fields. Yup gives us access to built in validators for several data types, like strings, arrays, and numbers. When we combine these features with a couple of other tools in react we can create a powerful controlled form like the one I created for my Boba Tea Project. Here's the snippet:&lt;/p&gt;

&lt;p&gt;`import React, { useState } from 'react'&lt;br&gt;
import { useFormik } from 'formik'&lt;br&gt;
import * as yup from 'yup'&lt;br&gt;
import { headers } from '../globals'&lt;br&gt;
import { useNavigate } from 'react-router-dom'&lt;/p&gt;

&lt;p&gt;const TeaItemForm = ({ addTeaItem }) =&amp;gt; {&lt;br&gt;
    // const [title, setTitle] = useState("")&lt;br&gt;
    const [error, setError] = useState({})&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const navigate = useNavigate()

const initialValues = {
    name: ""
}

const validationSchema = yup.object({
    name: yup.string().min(3).max(50).required()
})

const handleSubmit = async values =&amp;gt; {
    const options = {
        method: "POST",
        headers,
        body: JSON.stringify(values)
    }
    const resp = await fetch("/api/teaItems", options)
    const data = await resp.json()
    if (resp.status !== 201) {
        setError({ data })
    } else {
        addTeaItem(data)
        navigate("/teaItems")
    }
}

const formik = useFormik({
    initialValues,
    validationSchema,
    onSubmit: handleSubmit,
    validateOnChange: false
})

return (
    &amp;lt;div&amp;gt;
        &amp;lt;h3&amp;gt;Create Tea&amp;lt;/h3&amp;gt;
        &amp;lt;p style={{ color: 'orange' }}&amp;gt; {formik.errors.error}&amp;lt;/p&amp;gt;
        &amp;lt;form onSubmit={formik.handleSubmit}&amp;gt;
            &amp;lt;div&amp;gt;
                &amp;lt;label htmlFor="name"&amp;gt;name:&amp;lt;/label&amp;gt;
                &amp;lt;input type="text" name="name" id="name" value={formik.values.name} onChange={formik.handleChange} /&amp;gt;
                &amp;lt;p style={{ color: 'purple' }}&amp;gt; {formik.errors.name}&amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt; &amp;lt;br /&amp;gt;
            &amp;lt;input type="submit" value="Create TeaItem" /&amp;gt;
        &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}`&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Decorators 101:</title>
      <dc:creator>Carnegie Hall</dc:creator>
      <pubDate>Fri, 25 Oct 2024 13:13:43 +0000</pubDate>
      <link>https://dev.to/alegendcodes/decorators-101-399l</link>
      <guid>https://dev.to/alegendcodes/decorators-101-399l</guid>
      <description>&lt;p&gt;Decorators are a type of special function that allows us to add functionality to an object without modifying it's structure. &lt;/p&gt;

&lt;p&gt;When we design a decorator I like to think of it akin to Christmas wrapping. Don't leave just yet, let me explain! First to break it down we have a function stored within another function. &lt;br&gt;
The first function is our decorator function. &lt;br&gt;
The second function is our stored function or rather the inner function is our callback function that we use.&lt;/p&gt;

&lt;p&gt;The third function is our caller function.&lt;br&gt;
To use our caller function we need a variable set up to actually get our function within the decorator. &lt;/p&gt;

&lt;p&gt;Here we create an arbitrary name that sets everything in place for us to have our function become fully fleshed out. &lt;/p&gt;

&lt;p&gt;Here we have our variable store our decorator function. Get_this = decorator(function name) &lt;br&gt;
Within our decorator function we pass our third function into it as a callback function. &lt;/p&gt;

&lt;p&gt;This tells our decorator function, that the func potion is now taking in our get called() function and stores it in the 2nd func field. &lt;/p&gt;

&lt;p&gt;Now all we have to do is call our stored function.&lt;/p&gt;

&lt;p&gt;Imagine you have a box but with that box you want to store a christmas gift for a friend, sweetheart, or a family member.&lt;br&gt;
Our gift is the function we're calling within the second function, that also says that person's name. &lt;br&gt;
Our second function is the box that's keeping our gift safe, where we use our prints() as stuffing paper. &lt;br&gt;
Our decorator function is the nice note / wrapping paper that says the person's name as well. &lt;br&gt;
Our variable serves as the tape on the outside to keep our wrapping paper working and pretty. &lt;br&gt;
When we call our function that's done from the message on the wrapping paper. &lt;/p&gt;

&lt;p&gt;Now let's say we ran out of wrapping paper and only had enough to our 5 most important people for the holiday season. Here is where decorators are very special, we can reuse the same type of box, and wrapping paper to put a different gift inside of it for each of our very important people! I hope this was a good way to help you understand Decorator functions. &lt;/p&gt;

&lt;p&gt;A decorator is a function that takes another function as an argument and returns a new function, often attaching pre- or post-processing functionality.&lt;/p&gt;

&lt;p&gt;_ are referred to as underlines and mean our variable is private &lt;/p&gt;

&lt;p&gt;We can use try in our entire call to attempt to push the code. &lt;br&gt;
Then we can use except ValueError as error:&lt;br&gt;
Print(f"Error: {str(error)}")&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;rpr&lt;/strong&gt; representation method&lt;/p&gt;

&lt;p&gt;@classmethod&lt;br&gt;
Def print_player_names(cls): #class represents class &lt;/p&gt;

&lt;p&gt;For player in cls.all:&lt;br&gt;
Print(player.name) &lt;/p&gt;

&lt;p&gt;Player.print_player_names()&lt;/p&gt;

&lt;p&gt;Classmethods always should call on the class and not the individual item stored in the class. When calling a class capitalize it exactly as it is written in the class. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Props Basics: Part 1</title>
      <dc:creator>Carnegie Hall</dc:creator>
      <pubDate>Fri, 04 Oct 2024 01:10:00 +0000</pubDate>
      <link>https://dev.to/alegendcodes/props-basics-part-1-1mmk</link>
      <guid>https://dev.to/alegendcodes/props-basics-part-1-1mmk</guid>
      <description>&lt;p&gt;This is a beginner friendly tutorial on how to use props. It is important that you understand what destructuring is and how to use / create components before reading. &lt;/p&gt;

&lt;p&gt;Props, short for properties, props allow us to send information from parent components to child components, it's also important to note that they can be any data type. &lt;/p&gt;

&lt;p&gt;It is imperative to understand that the syntax for creating a prop for any of your components. In React, you must use the same syntax for writing an attribute for an html tag. The way we specify a prop is by putting it within our element like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ParentPlant() {
  return &amp;lt;ChildPlant text="Hey Farmer! Says plant" number={2} isPlant={true} /&amp;gt; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A good rule to remember when creating Props are: strings don't need to have curly braces around their value, we only do this for other data types. As you can see above we can have a multitude of props by assigning them to the parent component. When we have our props within our component it is important to know that we are technically &lt;em&gt;passing them down&lt;/em&gt;. Once we pass our props down we need to be able to receive them within our desired component. In this case our ChildPlant component.&lt;/p&gt;

&lt;p&gt;Receiving Props:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ChildPlant(props) {
  return (
    &amp;lt;&amp;gt;
      {props.text} {props.number}
    &amp;lt;/&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're doing to things here: 1. We are receiving our prop within the parameter of our ChildPlant component, 2. We are destructuring our prop's values via the name of our prop. It's important to know that our props &lt;em&gt;isn't&lt;/em&gt; a parameter but more so functions similar to one. &lt;/p&gt;

&lt;p&gt;The only way to pass parent component data down to it's child component is via props. I like to think of it like DNA, a parent component holds aspects of itself already existing within it. Because that child also can have aspects of it's parent's DNA, props works like the activator that makes that child's hair red, black, or blond. &lt;/p&gt;

&lt;p&gt;Props are received in the child function and are sent via the parent function, but props can only be sent down and never sent back up. We can think of props as objects as well. This is because they essentially hold data in them akin to key:value pairs. To touch back on why they are similar to parameters is that they they are storing multiple objects within them. I like to think of the area where props are received as placeholders. They take up space for some object that we want to share with our component and swap out when we need them to, via destructuring and dot notation. &lt;/p&gt;

&lt;p&gt;Here's a good way to visualize it: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff03fmwrwet07jd3cao8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff03fmwrwet07jd3cao8w.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we can visualize props holding everything within our square boxes, each one holding it's respective data value from our first example. Now we just use our destructuring method to grab the value of our prop. And that's how to use props in a nutshell!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Understanding POST requests in Javascript</title>
      <dc:creator>Carnegie Hall</dc:creator>
      <pubDate>Wed, 11 Sep 2024 00:58:23 +0000</pubDate>
      <link>https://dev.to/alegendcodes/understanding-post-requests-in-javascript-214p</link>
      <guid>https://dev.to/alegendcodes/understanding-post-requests-in-javascript-214p</guid>
      <description>&lt;p&gt;&lt;strong&gt;POST Requests:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This guide is meant for beginners who have a small amount of experience utilizing HTML, JavaScript, and interacting with web servers such as db.json and understand how GET Requests work. &lt;/p&gt;

&lt;p&gt;When creating a POST Request we start with the same format as a GET Request in which we define a URL: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch("")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We then need to let that fetch know that it is performing a POST so we have to pass in a second argument for it, making that argument be an Object: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch("",{}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We don't close our first parentheses yet, as we want to keep our data open for that second argument. &lt;/p&gt;

&lt;p&gt;The Object has to have a method within it so that the fetch knows which HTTP verb to send, because we are storing the method as the standard key:value it looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch("",{&lt;br&gt;
Method: "POST", (this particular value has to be in a string in all caps, we specify this comma to let JavaScript know we're done with this line)&lt;br&gt;
})&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Now we need to specify to our request the type of data that it will be sending to our Server. We do this by creating another Object, which is our headers: {}&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
fetch("",{&lt;br&gt;
Method: "POST",&lt;br&gt;
Headers: {}&lt;br&gt;
})&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Using the key:value standard we can then specify the content-type: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch("",{&lt;br&gt;
Method: "POST"&lt;br&gt;
Headers: {&lt;br&gt;
'Content-Type': 'application/json'&lt;br&gt;
},&lt;br&gt;
})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We now need a body to send this information across the internet, as you know, we shouldn't just send this as plain JavaScript text because we cant be certain it will be properly parsed and read by the server, the best thing to do in this instance is to send the JavaScript info pre-parsed to our server. We do this by using JSON.stringify and just pass in our function:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch("",{&lt;br&gt;
Method: "POST"&lt;br&gt;
Headers: {&lt;br&gt;
'Content-Type': 'application/json'&lt;br&gt;
},&lt;br&gt;
body:JSON.stringify(your function)&lt;br&gt;
})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Our Server takes this requests and processes it. It then places it within our local db.json. After that the Server sends a response back that will include our function and a new id for it as well. &lt;/p&gt;

&lt;p&gt;Once this happens we take our response(resp) and parse it once again, this time for our JavaScript to be able to manipulate by using the .json()method. Once that data is parsed we then can rename it as anything for it's element. This helps us to not only specify a non-generic name but helps us to think of what we'll do with our information. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch("",{&lt;br&gt;
Method: "POST"&lt;br&gt;
Headers: {&lt;br&gt;
'Content-Type': 'application/json'&lt;br&gt;
},&lt;br&gt;
body:JSON.stringify(your function)&lt;br&gt;
})&lt;br&gt;
.then (resp =&amp;gt; resp.json())&lt;br&gt;
.then(your parsed data =&amp;gt; (whatever you want to have the result of your parsed data as)&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Your final result should look somewhat like this, if you chose to take your newly parsed data and pass it through a function: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;function newPlayer(newForm) {&lt;br&gt;
  fetch("http://localhost:3000/Players", {&lt;br&gt;
    method: "POST",&lt;br&gt;
    headers: {&lt;br&gt;
      "Content-Type": "application/json",&lt;br&gt;
    },&lt;br&gt;
    body: JSON.stringify(newForm),&lt;br&gt;
  })&lt;br&gt;
    .then((resp) =&amp;gt; resp.json())&lt;br&gt;
    //   .then(player =&amp;gt; console.log(player))&lt;br&gt;
    .then((player) =&amp;gt; showPlayer(player));&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I often remind myself that not all information needs to be refactored. I currently attend a bootcamp and know how difficult it can be to take in loads of information, and the difficulty of time management to keep from getting overwhelmed. I hope this guide helps you to better understand POST requests and was simple and sweet and thanks for the read!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
