Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 29, 2022 12:01 am GMT

I Built an Online Collaborative Canvas using Redis as my Primary Database

Overview of My Submission

This week, I built Collanvas, a collaborative drawing platform with no login required, and where every user has access to only one color.

I created Collanvas for various reasons, one of them is brainstorming. Go draw the next big thing!

You can also use Collanvas for art. With one color per person, each one can focus on a specific part of the design.

And of course, you can also play games on Collanvas. With the chat feature, play draw-and-guess with your mates until your energy is full again.

Submission Category:

MERN Mavericks

Language Used

JS/TS/Node.js

Link to Code

GitHub logo eludadev / collanvas

Your whole team, changing the world one stroke at a time.

Collanvas Your whole team, changing the world one stroke at a time

With an online whiteboard, you can brainstorm , draw art , and even play games with your teammates and your friends. No login is required, and each user has access to the chatroom, a real-time messaging platform to have all kinds of discussions. Choose your own username, pick your own color palette, and go draw the next big thing!

Collanvas Home Page

Collanvas Room Page with a drawing of the text 'Redis' and a smiley face, and 2 chat messages from different people spelling 'Woohoo!'

How it works

How the data is stored:

Strokes data model

DataDescriptionType
ColorThe color of the strokeString (HEX)
ThicknessThe thickness of the strokeNumber (1 to 10)
TypeWhether stroke is pen or eraserString ('Pen' or 'Eraser')
PointsThe points that make up the stroke{x: Number,y: Number}[]

Keys are generates like canvas:{roomKey}. For each generated key, data is stored by running the ARRAPPEND command like: JSON.ARRAPPEND canvas:{roomKey}

Additional Resources / Info

How did I build Collanvas? Let's get technical.
Were storing two JSON documents per room:

Strokes db model

  1. The first one is strokes, a collection of every stroke on the canvas. We collect the stroke color, thickness, type, and the set of points that make it up. Type dictates whether the stroke is in pen-mode or eraser-mode.

Messages db model

  1. Next and last document is messages, an array of every sent message in the room. We collect the user-name, user-color and the content of the message.

Were using Redis Insight to inspect our database and confirm our data modeling strategy.

Drawing Done event

But when should we start saving data in these documents? Well, firstly, when the user lifts their pointer from the canvas, we add the drawn stroke to the Redis JSON store with the arrAppend command.

Message event

Lastly, when the user sends a new message, thats when we save their thoughts to the document with the same command.

API Route

Okay, but why are we saving all this data? Well, when the user first joins a room, we get them started by reading every stroke and message from the database and inserting them in their website.

Websockets

Sounds good, but how do we update Collanvas for every user in real-time? Well, were using web-sockets to create a channel of communication between the server and the browser. When the server receives information that, say, a new message just got sent, it will feed that information back into the browser and it will be added without reloading the page.

Redis PubSub

But how do we make different socket connections talk with each other? Redis solves this problem through their Publisher-Subscriber service. Each connection subscribes and publishes to its own two channels of messages and strokes, relaying all information to the browser.


Original Link: https://dev.to/eludadev/i-built-an-online-collaborative-canvas-using-redis-as-my-primary-database-2001

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To