Skip to main content

Command Palette

Search for a command to run...

Websocket - Redis

Updated
5 min read

Server serve something, something means - serve your request that why we called sever its like a while true loop, always list of requests

User is the one who open the request Server is actively listening the request, hence he took the user request

after some processed, lookup, tranform opertion return back response to user

And client is the closet connection

Dataflow Visulization

WebSocket is that is kind of transport layer, which your connection make duplex

user send http request and says convert into websocket. sever accept it and upgrade it to websocket. and this is my connection don't close it

now this is connection that is fully duplex
any one any time any side send the data and any one can close the connection

you will send a message or user will send u a message nothing apart from this happened

node libery is available npm i ws

Are you thinking, message will send to all the connection are open on server instead of 1 user

to handle this, there is library SocketIO - build by Vercel founder

This library provide the extra Abstraction

In this abstraction - assign the ID on socket, Rooms are there and also, you can identify uniquely to each socket

How does socket IO works
Assume all users are connected to websocket

Now socketIO what does do, he called each user to as a individual Socket and name it to server as IO

and automatically internally assging ID to each socket

how many socket connection handle by 1 single machine ?

=> Limited , its depend upon machine configuration, you can not give the precise number.

In machine there is handle limited number of connection that means it will break over the time when excuse the limit. HOW CAN SOLVE THIS PROBLEM?

Suppose my machine comnction limit is 1000x, once reach at 800x,I will increase my machine size === VERICAL SCALING

What is vetical scaling problem ?

1.You need the shut down the machine then add new RAM, HDD and resources 2.you can not upgrade RAM, CPU in running Machine

3. You need to disconnect the connected user (connection) then you can upgrade

What is horizontal scaling ? When user reach at the near limit of connection add new machine.

What is problem when you scale the horizontal machine?

Machine 1 \(env:PORT=8000; node index.js \)env:PORT=8000; node index.js

Machine 2 $env:PORT=9000; node index.js

Machine 1 both connection is sync each other but Machine 2 not synch with Machine 1

State not update websocket not linked to another server

because WebSocket is Stateful connection

9000 server user is not connected to the 8000 machine application. meanwhile 9000 machine user has same application

Means if there is any event came kindly tell my boradcaster only

if you check multiple user with 9000, they are interconnected and state are updated

To Tackle this, required add broker server so that communication will multi layer

we can scaling the server using this approach For the broker server we can use REDDIS,


REDIS

How Does Redis work ?
Redis works on key value pairs,
when we want ot in syetem ti chache as remote dectionary.
also has pubsub, add on [XADD, XREAD] for streaming.
best result for streaming use KAFAK

Three two type of memory
1.Stack
2.Heap
both are store the Primary me (RAM)

Stack is very fast, because in heap you have to search the space for allocation.

Redis is work on In meomory principle, I will allocate the memory from heap memory I store in it, when you want, you will get that data from it
This process work on primary stoarage ie RAM so its very fast

and Redis also store the data prriodicaly in disc as a backup

Can we see this is problem every time I have to find space in heap to allocate memory to store data and transfer to stack, its very time consuming

To solve this problem Redis made the APPEND ONLY LOG SYSTEM
I will keep the data next allocation so that next time i will pickup my last reference for next allocation to store

Why Redis ?
REDDIS has the pubsub feature (publisher subscriber), secondary approach VALLKEY both are same

we are using valkey as a socket server, who make a connection to socket

ioreddis libery to connect

We can open the publish channel to send data to Redis we can subscribe(received) the data from same channel. its limitation of Redis

That means we have to open the two conection 1. Publish 2. Subscribe

io.on('connection', (socket)=>{ console.log(Socket connected, {id: socket.id})

    socket.on('client:checkbox:change', (data)=>{
        console.log(`[Socket : ${socket.id}] : client:checkbox:change`, data)
        // io.emit('server:checkbox:change', data)
        // state.checkboxes[data.index] = data.checked;

        await publisher.publish(
            'internal-server:checkbox:change',
            JSON.stringify(data)
        )
    })
})

suppose Redis is broker sever they should subscribe to Redis

1 views