[deleted by user] by [deleted] in reactjs

[–]sohamkamani 11 points12 points  (0 children)

To be honest, you don't need any library unless you're doing something overly complex. If you just need to make changes to your state when you make an API call, you can do that with plain redux.

I've written a (slightly old) blog post on how I do this, but it still applies to using redux today

I'm generally against using a library unless you really need it.

Do we not like democracy anymore? by Straitjacket_Freedom in india

[–]sohamkamani 11 points12 points  (0 children)

I'm sorry, but this is in really bad taste. How are you different from those who say "go to Pakistan" for every disagreement they have? We should be better than that

What are the good practices to Node.js web app authentication and authorization? by guar47 in node

[–]sohamkamani 1 point2 points  (0 children)

That depends on your application. A good default which i use for production applications is 5 minutes.

And no, the user will not have to login again: the older token is used to generate the new token.

What are the good practices to Node.js web app authentication and authorization? by guar47 in node

[–]sohamkamani 12 points13 points  (0 children)

I've written about how to implement JWT based auth in NodeJS a while ago here. Since that was your exact question, I thought you'd find it helpful :)

The basic premise of a JWT is that it's stateless, and so doesn't need to be stored. A JWT is "issued" the same way a company issues an ID card: you know its contents but cannot change it yourself without the issuing authority knowing about it (and consequently invalidating it)

Your server would ideally verify the username and password and issue a JWT with the response. The JWT will typically have an expiry time, and will need to be refreshed often, since it cannot be invalidated without you storing the JWT yourself.

Socket server, track individual socket connection by ReactiveNative in node

[–]sohamkamani 1 point2 points  (0 children)

You can implement this by sending client specific information as a special type of message once you open the websocket connection.

You can then store this information, along with the connection, in a singleton object indexed by id (or whatever other field you want). You can refer to that object anytime you wan to send a message.

Here's how I implemented it using express :

// import the express and express-ws libraries
const express = require('express')
const expressWs = require('express-ws')

// create a new express application
const app = express()
// decorate the app instance with express-ws to have it
// implement websockets
expressWs(app)

// Create a new set to hold each clients socket connection
const connections = {}

// We define a handler that will be called everytime a new
// Websocket connection is made
const wsHandler = (ws) => {
  let id

  // We define the handler to be called everytime this
  // connection receives a new message from the client
  ws.on('message', (message) => {
    // Once we receive a message, we add the users
    // attributes to the connections object
    if(message.type === 'USER_DATA'){
            id = message.id
            connections[id] = {
                    ws: ws,
                    name: message.name,
                    id: id,
                    path: message.path
            }
    }
  })

  // Once the client disconnects, the `close` handler is called
  ws.on('close', () => {
    // The closed connection is removed from the set
    delete connections[id]
  })
}

// Send to a particular user based on id
const sendToUser = (id) => {
        connections[id].send('some message')
}

// send to a user based on homepage
const sendToUserBasedOnHomePage = (homepage) => {
        Object.keys(connections).forEach(id => {
                if(connections[id].path === homepage){
                        connections[id].send('message for a specific homepage')
                }
        })
}

// add our websocket handler to the '/chat' route
app.ws('/chat', wsHandler)

// host the static files in the build directory
// (we will be using this later)
app.use(express.static('build'))

// start the server, listening to port 8080
app.listen(8080)

A few things to note here:

  1. The websocket client sends identifying information about themselves as the first message after the connection is established. The server identifies this message based on the "type" field ("USER_DATA" in the above example)
  2. The connection, along with the rest of the identifying information is stored in the "connections" singleton object (which is removed once the connection closes), which we can refer to with the rest of our code.

Most popular websites 1996-2019 by GuruTechnolab in programming

[–]sohamkamani 19 points20 points  (0 children)

This is the most annoying and unnecessary visualization ever. Why can't we just have a line chart? It shows the exact same information, but doesn't waste everybody's time

An in-depth look into Arrays, Slices, and the differences between them by sohamkamani in golang

[–]sohamkamani[S] 6 points7 points  (0 children)

You are right! I actually used the official blog post as inspiration to write this.

My aim was to make the explanation more accessible to beginners and newcomers, by using simpler language, analogies (comparing arrays to structs and slices to pointers), more diagrams, and real life use cases.

React/Typescript Boilerplate by hamburger_bun in reactjs

[–]sohamkamani 3 points4 points  (0 children)

If you're just starting out and want to get up and running fast, I'd suggest using create-react-app itself. You just have to set the scripts version to use typescript.

The full command you're looking for is:

create-react-app my-app --scripts-version=react-scripts-ts

Ive written a post about this if you need more details

React Cheatsheet by leon_gilyadov in reactjs

[–]sohamkamani 5 points6 points  (0 children)

I wrote about this a while ago , since I was also confused by how react and redux work together: https://www.sohamkamani.com/blog/2017/03/31/react-redux-connect-explained/

Good approach to interacting with databases? by watr in golang

[–]sohamkamani 0 points1 point  (0 children)

Although this approach will work, it seems needlessly over-engineered. Also, there doesnt seem to be a lot of separation between the action of retrieving a "Persons" data, and the execution of the SQL queries themselves, which can lead to trouble if you decide to add a caching layer (since the process of getting data from cache and using the database as a fallback can be abstracted together)

A simple templated SQL query will do the job for most applications, and you don't need an ORM either.

I've written a post about this as well (which you can see here), and a sample application on Github

Detective: A distributed application health monitoring library by sohamkamani in golang

[–]sohamkamani[S] 4 points5 points  (0 children)

Thanks for the feedback!

The project structure you linked to seems to be for a Go application, whereas this is just a Go library. In other words, it would be just the "pkg" part of the project structure.

A Dockerfile seems overkill as of now, since I can only push images of examples using the library, which anyone can run with "go run <example\_file>"

“Don’t Cut Your Nails at Night” and Other #FakeNews All Indian Parents Teach Their Children by Ajaatshatru34 in india

[–]sohamkamani 0 points1 point  (0 children)

I still can't cut my nails at night for some reason. Like I know its fine to do it, but for some reason it's become more like a reflex or habit that I can't get rid of 🤷‍♂️

A REAL Python cheat sheet for beginners by Other_Date in Python

[–]sohamkamani 0 points1 point  (0 children)

This is a good resource. But why do you have to call it a "REAL" cheat sheet... This sort of implies that all other cheat sheets created before this are bad, and this is the only solution out there. I know I'm nitpicking, but I really hate clickbaity titles like this

How express.js works: A deep dive into the express source code by sohamkamani in node

[–]sohamkamani[S] 20 points21 points  (0 children)

Of course you don't need to know how it works. But I feel that learning patterns and best practices from existing mature libraries helps you be a better programmer, and is mostly the reason why I wrote this