Web Experiment: Shared canvas with Websockets

For today experiment I tryed to recreate the cool sketchpad by mr doob using websockets and HTML5Canvas.

The idea is that while you paint on the canvas your strokes are send to the server and broadcast to all the users on the web in realtime using websockets, you also can see the cursor of the other people. Click on the image to test it but remember that if there are not other users online you will be just drawing alone.

Shared Canvas

If you want to know more about the coding read the full post.

Server side

I’ve been trying for a long time to make an app using websockets, but it turned out to be harder than it should be, mainly because the standard has been changing constantly for the last year so it was hard to find libraries to use on the server side to abstract me from the protocol, I even tryed to coded by myself but there was some binary info being exchanged that I couldnt handle.

I wanted to code a simple broadcast server. I’ve done it before in python but for this time I wanted to use node.js, I really think that nodejs is a great idea. I understand why some people hesitate, but the truth is that it allows to prototype apps really fast thanks to a good API and the benefits of using the same code on the server and the client.

I’ve tryed to code a broadcast server in nodejs before, but all the libraries I tryed were giving me problems during installation, even using the node package manager. Everybody recommends to use socket.IO but I find that library too big and complex to use (it forces you to serve files to fallback in case the browser doesnt support websockets, which wasnt my case).

Finally I found one called faye-websockets that worked perfectly, nice sintax and support most of the version of the standard. It even creates an http server so you can use the same server to handle static files.

The server is really simple, every message gets resend to all the clients connected, it even allows to buffer the latest 100 messages so you can replicate the last part of the session when someone gets connected.

I was surprised by the performance, it will be nice to make a benchmark with several users but right now it seems it can handle a lot of concurrent users without problems.

And the coolest thing is that the server is totally blind to the app, so I can reuse it for any app I want in the future. I’m improving it so it allows a tiny protocol to store and retrieve info from the server.

Client side

Here nothing new, the user actions are transformed in messages and then processed by the painting app and sent to the server, so I dont have to code the behaviour of the users actions separately.

I experimented with different types of brushes but the HTML5 Canvas is limited, I wanted to tint an image and use it as a brush but the Canvas doesnt allow to tint an image, so I discarded that option).

The cool thing is that now I can add new types of user actions and the platform doesnt change, so to prove that I did cursor rendering on the screen of the other online users.

Persistent data

There is only one problem about the app, new users cant see the previous image (besides the buffered strokes which are just a few), so I’ve been thinking of an option that let the users send to the server the canvas image so the new users can download it, I can do that easily using the http server included with the library.

The server sets a DB where it can store data (just a name=>value storage). Clients can send data with a given name or retrieve it. This info is not persistent once the server is restarted (it can be easily dumped to a text file but I dont need it by now).

The only problem was that due to cross-server policies I wasnt able to access the HTTP server through ajax calls if the server were I hosted the app was not the own websocket-http server (which wasnt).

Luckly thanks to this wonderful site I was able to set the CORS policy info on the HTTP response header very easily just adding this field:

response.writeHead(status_code, {'Content-Type': 'text/plain', "Access-Control-Allow-Origin":"*"});

Another nice feature is that I can access my server through the browser typing an special url to check how many clients are online, useful to debug purposes. I even could create a tiny admin protocol to handle stuff.

 

Comments

2,967 responses to “Web Experiment: Shared canvas with Websockets”

  1. wholesale guest wear Al Ain Avatar

    When someone writes an post he/she retains the idea of a user in his/her mind that how a user can know it.

    Thus that’s why this post is great. Thanks!

  2. Embroidery Avatar

    Hello it’s me, I am also visiting this site daily, this site is really
    nice and the viewers are genuinely sharing nice thoughts.

  3. join ukraine war Avatar

    Greetings! I know this is kinda off topic nevertheless I’d figured I’d ask.
    Would you be interested in trading links or maybe guest writing a blog article or vice-versa?
    My blog addresses a lot of the same subjects as yours and I
    believe we could greatly benefit from each other.
    If you’re interested feel free to send me an email.

    I look forward to hearing from you! Excellent blog by the way!

  4. phok casino bangladesh Avatar

    Link exchange is nothing else but it is only placing the other person’s web site link on your page at appropriate place and other person will also do similar for you.

  5. Advanced Seo Avatar

    Why Is SEO Important in Content Writing? This is the reason most entrepreneurs focus their efforts on improving Google rankings.
    Why select OutreachCrayon for area of interest edits?

    Diverse area of interest protection. Multiple authority levels out there.
    Hosting one related to your area of interest establishes authority whereas piquing viewers curiosity; who wouldn’t want expert advice?
    If you wish to know extra about me, it’s
    also possible to go to my weblog and web site, try
    my GitHub profile or even higher: comply with @thorstenball
    on Twitter. If AI models are utilizing sure sources, you wish to be one among them-via clear, authoritative content material and properly-optimised structured data.

    However, the 2 most vital elements are excessive-quality content and backlinks
    (incoming hyperlinks from different web sites).
    However, new alternatives might emerge, like AI-pushed procuring suggestions or voice-based search advertisements.
    However, these benefits stop as quickly as you stop paying for ads.
    Quality links earn high quality results, and that’s what you’ll get when leveraging our link analysis companies backed by RevenueCloudFX, our in-house advertising know-how.
    Glasgow Web Design offer a wide range of skilled SEO companies to our clients throughout Glasgow,
    Scotland and the UK and Ireland.

  6. ????? ?????? ??????????? Avatar

    Wow, amazing weblog layout! How long have you been blogging for?
    you made blogging glance easy. The total look of your
    web site is excellent, as neatly as the content!

  7. ????? ?????? ??????????? ???? ?? ??????? Avatar

    Hey There. I discovered your weblog the use of msn. This
    is a very smartly written article. I will be
    sure to bookmark it and come back to read more of your helpful
    info. Thank you for the post. I’ll definitely comeback.

  8. the dark pages Avatar

    If you wish for to get a good deal from this piece of writing then you have to apply these strategies to your won web site.

  9. a href checker Avatar

    Investors Google startups earlier than taking meetings.
    It builds Seo authority, creates indexed content material, and establishes the belief signals that fashionable buyers and buyers look for.
    These on-page Seo methods enhance both webpage usability and
    search engine rankings. These keywords are the actual words or phrases that users use when typing
    a query right into a search engine. Strategic niche edits.
    Find present articles that already rank for key phrases relevant to your
    product. New product line, convention push, funding
    announcement. Product roundups and listicles create everlasting backlinks and drive certified referral site visitors
    from customers actively trying to find options in your class.
    Consider a part-time PR contractor upon getting validated the
    impact of your digital PR basis and might point to concrete results
    resembling backlinks earned, site visitors grown, and deals influenced by third-occasion coverage.
    Get it flawed and also you both burn money on clicks that never close, otherwise
    you wait eighteen months for natural traffic that
    arrives after the cash runs out. Months 7 to 12: Minimal budget ($100 to $200 monthly).
    By month six, their domain had acquired 18 referring domains completely by way of free
    and low-cost tactics. Digital PR can be started
    immediately by a solo founder with a few centered hours
    per week, and much of it is completely free.

  10. berita forex hari ini Avatar

    What’s up Dear, are you really visiting this site on a regular basis, if so after that you will absolutely take nice experience.

  11. babu 88 casino bangladesh Avatar

    Hey I am so grateful I found your website, I really found you by error, while I was searching on Yahoo for something else, Nonetheless I am
    here now and would just like to say thanks for a remarkable post and a all round enjoyable blog (I also love the theme/design),
    I don’t have time to read it all at the moment but I have book-marked it and also
    included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the great
    work.

  12. webpage Avatar

    Thanks a bunch for sharing this with all folks you really recognize
    what you are speaking approximately! Bookmarked. Please additionally
    talk over with my web site =). We can have a link alternate contract
    between us

  13. ????? ?????? ??????? ?? ??????? Avatar

    Hello there, just became aware of your blog through Google, and found that it’s really informative.
    I’m gonna watch out for brussels. I’ll appreciate if you
    continue this in future. Many people will be benefited from
    your writing. Cheers!

  14. table runners ivy pattern collection supplier UAE Avatar

    I have been exploring for a little bit for any high quality articles or weblog posts on this sort of house .
    Exploring in Yahoo I finally stumbled upon this site.
    Studying this info So i’m glad to show that I’ve a very good
    uncanny feeling I found out exactly what I needed. I most surely
    will make sure to don?t disregard this website and give it a look regularly.

  15. DreamProxies.com Avatar

    Great info. Lucky me I found your site by accident (stumbleupon).

    I have book-marked it for later!

  16. Come pianificare lo scambio di più volti in una foto di gruppo Avatar

    Per informazioni su Come pianificare lo scambio
    di più volti in una foto di gruppo, la nostra pagina AI Swap Face è https://aiswapface.ai/it/blog/multiple-face-swap-guide/. Gestiamo
    questa risorsa. Per un risultato nitido, usa una foto sorgente ben illuminata con un angolo del volto simile a quello della foto di destinazione.

  17. ?????? ?????? ??????? Avatar

    After I initially left a comment I appear to have clicked the
    -Notify me when new comments are added- checkbox and from now on every time a comment is added I get four emails with the exact same comment.
    Perhaps there is an easy method you can remove me from
    that service? Thank you!

  18. beautiful tron address Avatar

    Magnificent beat ! I wish to apprentice even as you amend your
    website, how can i subscribe for a blog website? The
    account aided me a acceptable deal. I were a little bit familiar of this your broadcast offered brilliant clear idea

  19. ???? ??????? ?????? ?????? Avatar

    Hi, I would like to subscribe for this blog to obtain latest updates, thus where can i
    do it please assist.

  20. webpage Avatar

    You have made some decent points there. I checked on the net for additional information about
    the issue and found most people will go along with your views
    on this website.

  21. webpage Avatar

    It’s amazing to go to see this site and reading the views of all
    colleagues regarding this paragraph, while I am also zealous of getting know-how.

  22. ??????? ?????? ?????? Avatar

    It’s in fact very complicated in this active life to listen news on Television, therefore I just use the web
    for that reason, and obtain the hottest information.

  23. ok9 Avatar

    With havin so much written content do you ever run into any problems of plagorism or copyright
    infringement? My site has a lot of unique content I’ve either created myself or outsourced but
    it appears a lot of it is popping it up all over the
    internet without my agreement. Do you know any ways to help prevent content from being stolen? I’d
    really appreciate it.

  24. Search Engine Optimization (SEO) Avatar

    If you are going for best contents like I do, only pay a quick visit this website everyday
    for the reason that it presents quality contents, thanks

  25. make bomb manual Avatar

    Can I simply say what a comfort to find someone that truly understands what they
    are discussing on the net. You actually realize how to bring an issue to light and make it important.
    More people must look at this and understand this side of
    the story. It’s surprising you are not more
    popular since you surely have the gift.

  26. ?????????? ???????? ?????? ?? ???? ???? Avatar

    ????????? ??????????????? ???????
    ?? ???? ? ?????? ???????? ??? ?????? ? ?????????? ????? ?????????? ???????? ?????? ?? ???? ???? ???????????
    ? ??? ??? ????????? ????????
    ?????? ? ???????? ??????????!

  27. My Video Avatar

    With havin so much content and articles do you ever run into any issues of plagorism or copyright violation? My website has a
    lot of unique content I’ve either created myself or outsourced but it looks like a lot of it
    is popping it up all over the web without my authorization. Do you know any techniques to help prevent content from
    being ripped off? I’d definitely appreciate it.

  28. Jordanemown Avatar

    ????????????? ?????: ??? ???, ???????? ????????????? ? ????????

    ?????????????? ? ????????????? ???????? (??????, ???????? ??????, ?????? ??????, ???????) https://prokamny.ru/our-portfolio/

  29. 10 euro gratis Avatar

    Hello, I log on to your blogs regularly. Your writing style is witty, keep up
    the good work!

  30. seo backlinks explained Avatar

    I have been browsing online more than three hours as of late, yet I
    never found any attention-grabbing article like yours. It’s beautiful
    worth sufficient for me. Personally, if all web
    owners and bloggers made excellent content material as
    you probably did, the net shall be a lot more helpful
    than ever before.

  31. ????????? ?????? ?????? ??????????? ???? Avatar

    Excellent goods from you, man. I’ve take
    into account your stuff previous to and you’re
    just too fantastic. I actually like what you have acquired right
    here, really like what you’re stating and the way by which you assert it.
    You are making it entertaining and you continue to care for
    to stay it smart. I can not wait to learn much more from you.
    This is really a tremendous website.

  32. ??? ???????? ? ????? Avatar

    Oh my goodness! Incredible article dude! Thanks, However I am having issues with your RSS.
    I don’t understand the reason why I cannot join it.
    Is there anybody having identical RSS issues?
    Anybody who knows the solution can you kindly respond? Thanx!!

  33. ?? ? ?????? ??????? ??????? ?? ???? Avatar

    Its such as you read my thoughts! You appear to understand so much approximately this, like you wrote the book in it or something.
    I feel that you simply could do with a few percent to drive the message
    home a bit, however instead of that, this is fantastic blog.
    An excellent read. I’ll certainly be back.

  34. c2689064545678956620 Avatar

    Good post however , I was wondering if you could write a litte more
    on this subject? I’d be very grateful if you could elaborate
    a little bit further. Kudos!

  35. comment-259018 Avatar

    Hi, i read your blog from time to time and i own a similar
    one and i was just wondering if you get a lot of spam responses?
    If so how do you prevent it, any plugin or anything you can recommend?
    I get so much lately it’s driving me mad so any help is very much appreciated.

  36. Content Delivery Network (CDN) Avatar

    If some one needs expert view on the topic of blogging after
    that i propose him/her to pay a visit this webpage, Keep up the nice
    job.

  37. https://456db-vip.com/ Avatar

    Hi, 456db hi?n là c?ng game uy tín v?i t?c ?? ?n ??nh.
    S?nh game nhi?u l?a ch?n t? x? s?, khuy?n mãi h?p d?n. Em
    ?ang ch?i r?t ?n, ai ?ang tìm nhà cái thì vào
    tr?i nghi?m nhé! https://456db-vip.com/

  38. web page Avatar

    Pretty part of content. I just stumbled upon your weblog and in accession capital
    to assert that I get actually enjoyed account your weblog
    posts. Any way I’ll be subscribing in your augment or even I
    achievement you get entry to constantly quickly.

  39. webpage Avatar

    I came across human hair wigs in Kenya and found Human Hair Hub.
    They focus on quality human hair at reasonable prices, which is great because a
    lot of sellers charge too much for basic styles.
    Anyone here bought from them before?

  40. buy xanax online Avatar

    I was suggested this web site by my cousin. I’m not sure whether
    this post is written by him as nobody else
    know such detailed about my problem. You are incredible! Thanks!

  41. filme porno române?ti Avatar

    O explica?ie mai detaliat? ar fi interesant?. Ar merita s? aprofund?m acest subiect.

  42. casino en ligne Avatar

    https://www.public.fr/meilleur-casino-en-ligne-france

    I’m not that much of a internet reader to be
    honest but your sites really nice, keep it up! I’ll go ahead and bookmark your website to
    come back later. Many thanks

  43. ???????? ?????? ?????? Avatar

    Outstanding post but I was wanting to know if you could write a litte
    more on this topic? I’d be very grateful if you could elaborate a little bit further.

    Appreciate it!

  44. 24kbet casino Avatar

    I seriously love your site.. Excellent colors & theme. Did you
    develop this site yourself? Please reply back as I’m
    wanting to create my own personal blog and
    would love to know where you got this from or what the theme is named.
    Thank you!

  45. casino-us-casino.com Avatar

    We are a group of volunteers and opening a new scheme in our
    community. Your site offered us with valuable info
    to work on. You’ve done a formidable job and our whole community will be grateful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *