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,965 responses to “Web Experiment: Shared canvas with Websockets”

  1. useful site Avatar

    Greetings, There’s no doubt that your blog could possibly be having
    web browser compatibility issues. Whenever I look at your blog in Safari, it looks fine but when opening in Internet Explorer, it’s got some overlapping
    issues. I just wanted to give you a quick heads up!
    Besides that, wonderful blog!

  2. Pizza Van Hire Avatar

    Hi, i feel that i saw you visited my website so i came to
    return the favor?.I am attempting to to find issues to
    enhance my website!I suppose its adequate to make use of a few of your ideas!!

    Look at my blog post … Pizza Van Hire

  3. click to read more Avatar

    Highly energetic post, I loved that bit. Will there be a part
    2?

  4. Recommended Site Avatar

    I just couldn’t depart your web site prior to suggesting that
    I really enjoyed the standard information an individual supply on your visitors?
    Is going to be back often to check out new posts

  5. v24 ?????? ??????????? ???? ?? ??????? Avatar

    Very good info. Lucky me I found your website by accident (stumbleupon).
    I have book-marked it for later!

  6. th?i ti?t hôm nay Avatar

    Nice blog here! Also your website loads up fast!

    What web host are you using? Can I get your affiliate link to your host?
    I wish my website loaded up as fast as yours lol

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

    Hey there! I simply would like to offer you a big thumbs up for the great
    information you’ve got right here on this post.

    I will be coming back to your website for more soon.

  8. ?????? ??????? ?????? ??????? ??????? ?????? ?????? Avatar

    ?????? ???????????? ???????? ???????? KRAKEN?

    ??????????? KRAKEN ???????? ??????? ?????????????? ????????? ????????? ????????? ???????? ????????.
    ??-??????, ??? ??????? ? ????????????? ???????????, ??????????????
    ??????? ?????????. ??-??????,
    ?????????? ???????? ????????? KRAKEN, ??????? ???????? ?????????, ????? ??????? ? ?????????? ????????
    ???? ??? ????? ?????????????.
    ?-???????, ??????????? ??????? ?????????? ??????????, ?????????? ????????? ?????????? ?????? (????????) ?
    ??????????? ????????????? ????????? ?????????????, ??? ????????????
    ????? ??? ????? ?????? ??????.
    ?? KRAKEN ???????????????? ?????????? ? ???????????? ?????????? ? ????????????
    ????????, ??? ?????? ??????? ??????? ????? ?????????????, ?????????? ?, ??? ?????????, ?????????? ????? ?????????????, ??????? ??????????? ? ??????????.

  9. https://japanese.xvideoszone.cc Avatar

    Nice blog! Is your theme custom msde or did yyou download it from somewhere?
    A desogn liike yoiurs with a few simple adjustements would really mazke my blogg jump out.
    Pleae let me know where you ggot your design. Many thanks
    xnxx

  10. ????? ?????? ?????? ??????????? ???? Avatar

    all the time i used to read smaller articles or reviews that also clear
    their motive, and that is also happening with
    this piece of writing which I am reading at this place.

  11. ?????? ?????? ??? ?????? Avatar

    Thank you for every other informative blog. Where else may just I
    get that type of information written in such
    an ideal way? I have a undertaking that I am simply now operating on, and I’ve been at
    the look out for such information.

  12. email infrastructure suite Avatar

    Your style is so unique in comparison to other people I’ve read
    stuff from. Thank you for posting when you’ve got the opportunity,
    Guess I will just bookmark this blog.

  13. https://sandiegobr.com/onlywin-casino-ethereum-payments-games-canada/ Avatar

    This is the best time to make plans for the future, and this is
    the time to be happy. I read this post and if I could, I would strive to suggest
    some interesting things or suggestions to you. Perhaps you could write
    the following articles referring to this article.
    I want to read even more about this!

  14. bandar 89 Avatar

    I think this is one of the most significant info for me.
    And i am glad reading your article. But wanna remark on few general
    things, The web site style is ideal, the articles is really
    nice : D. Good job, cheers

  15. nohu90 Avatar

    Howdy! This article couldn’t be written any better! Going through this article reminds me of my previous roommate!

    He continually kept preaching about this. I am going to
    forward this post to him. Fairly certain he’ll have
    a good read. Thanks for sharing!

  16. from Avatar

    Hi there! Would you mind if I share your blog with my myspace group?

    There’s a lot of folks that I think would really appreciate your content.
    Please let me know. Thank you

    Here is my web blog: from

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

    Hello mates, how is everything, and what you want to say regarding this post, in my view its actually awesome in favor of me.

  18. youtube playlist length calculator Avatar

    Hi, I think your website might be having browser compatibility
    issues. Whenever I take a look at your website in Safari, it
    looks fine however when opening in IE, it’s got some overlapping issues.
    I simply wanted to provide you with a quick heads up! Besides
    that, excellent site!

  19. site de rencontre Avatar

    https://lola-rencontre.fr/

    Hey there! This is my 1st comment here so I just wanted to give a
    quick shout out and tell you I truly enjoy reading your articles.

    Can you recommend any other blogs/websites/forums that cover the same subjects?
    Appreciate it!

  20. businessesforsale .com Avatar

    Yes! Finally something about #businesssales.

  21. sinyal trading terbaik Avatar

    I am in fact delighted to read this weblog posts
    which contains lots of valuable information, thanks for providing such data.

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

    Oh my goodness! Awesome article dude! Many thanks, However I am encountering troubles with your RSS.

    I don’t understand the reason why I can’t join it. Is there anyone
    else getting identical RSS issues? Anyone that knows the
    solution will you kindly respond? Thanks!!

  23. ?? ?? ?? Avatar

    Hi my friend! I want to say that this article is awesome,
    great written and come with approximately all vital infos.
    I would like to look extra posts like this .

  24. web site Avatar

    Hello, after reading this awesome article i am too cheerful to share my familiarity here with friends.

  25. association not for profit Avatar

    It’s amazing to go to see this site and reading the views of all friends concerning this paragraph, while I am
    also keen of getting experience.

  26. T? L? Kèo Nhà Cái Avatar

    Attractive section of content. I just stumbled upon your website and
    in accession capital to assert that I get actually enjoyed account your blog posts.
    Any way I’ll be subscribing to your feeds and even I
    achievement you access consistently fast.

  27. https://deshisexx.com Avatar

    Explore DeshiSexx for online entertainment and adult-oriented content.
    Visit the website to discover its available content and enjoy
    a user-friendly browsing experience.

  28. ????????? ???????? ? ?????? Avatar

    Thank you for another informative website. The place
    else may just I am getting that kind of information written in such an ideal way?
    I have a undertaking that I’m simply now operating on, and I have been on the look out for such info.

  29. ngentot Avatar

    I’m not that much of a online reader to be honest but your sites
    really nice, keep it up! I’ll go ahead and bookmark your website to come back down the road.
    All the best

  30. pipe stress Avatar

    Greetings! Very useful advice in this particular article!

    It is the little changes which will make the most important changes.

    Thanks for sharing!

    Feel free to visit my webpage :: pipe stress

  31. uy88 Avatar

    Hey, UUY889 là n?n t?ng uy tín v?i giao
    di?n hi?n ??i. Kho game ?a d?ng t? b?n cá, n?p
    rút nhanh. Mình th?y r?t ?n, anh em ?ang tìm nhà cái thì nên th?
    nhé! uy88

  32. web page Avatar

    With havin so much content do you ever run into any problems of plagorism or copyright
    violation? My site has a lot of completely unique content I’ve either authored
    myself or outsourced but it looks like a lot of it is popping
    it up all over the internet without my permission. Do you know any methods to help protect against content
    from being stolen? I’d truly appreciate it.

  33. ???? Avatar

    Hello just wanted to give you a brief heads up and
    let you know a few of the pictures aren’t loading properly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different browsers and both show the same outcome.

  34. ?????????? ?????? ?????? ??????????? ???? Avatar

    Great blog here! Also your web site loads up very fast!
    What host are you using? Can I get your affiliate
    link to your host? I wish my site loaded up as fast as yours lol

  35. business for sale services Avatar

    Pretty nice post. I just stumbled upon your weblog and
    wanted to say that I have truly enjoyed browsing your blog posts.
    After all I will be subscribing to your feed and I
    hope you write again very soon!

  36. ???? ?? ??? Avatar

    It’s going to be ending of mine day, except before finish
    I am reading this great article to improve my know-how.

  37. sell gold Avatar

    Pretty nice post. I just stumbled upon your blog and wished to say that I’ve truly enjoyed surfing around your blog posts.

    After all I’ll be subscribing to your rss feed and I hope you write again soon!

  38. ????? Avatar

    It’s an awesome article for all the web people; they will take
    benefit from it I am sure.

  39. pornoroliki.live Avatar

    An important consideration to explore is how the approach performs under different conditions.

  40. hello88 Avatar

    Hey, I think your website might be having browser compatibility issues.
    When I look at your blog site in Ie, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that,
    excellent blog!

    Check out my homepage – hello88

  41. corporate law firms in karachi Avatar

    Attractive section of content. I just stumbled upon your
    web site and in accession capital to assert that I get actually enjoyed account your blog posts.
    Any way I will be subscribing to your augment
    and even I achievement you access consistently quickly.

  42. free ai headshot generator free Avatar

    Pour des informations sur free ai headshot generator free, notre page AI
    Swap Face est https://aiswapface.ai/fr/ai-headshot/. Nous gérons cette ressource.
    Pour un résultat net, utilisez une photo source bien éclairée,
    avec un angle de visage proche de celui de la photo cible.

  43. Usa Private Proxies Avatar

    Link exchange is nothing else however it is simply placing the other person’s webpage
    link on your page at suitable place and other person will also do similar in support of you.

  44. clothes change ai Avatar

    Para informações sobre clothes change ai, nossa página AI Swap Face é https://aiswapface.ai/pt/ai-clothes-changer/. Nós mantemos esse recurso.
    Para um resultado nítido, use uma foto de origem bem iluminada, com um
    ângulo de rosto semelhante ao da imagem de destino.

  45. ?????? ???? ??????? ????????? ?????? ???? ??? Avatar

    ?????? ???????????? ???????? ????????
    KRAKEN?
    ??????????? KRAKEN ???????? ??????? ?????????????? ????????? ????????? ????????? ???????? ????????.
    ??-??????, ??? ??????? ? ????????????? ???????????,
    ?????????????? ??????? ?????????.
    ??-??????, ?????????? ???????? ????????? KRAKEN, ??????? ???????? ?????????,
    ????? ??????? ? ?????????? ???????? ???? ??? ????? ?????????????.
    ?-???????, ??????????? ??????? ?????????? ??????????, ?????????? ????????? ?????????? ?????? (????????) ? ??????????? ?????????????
    ????????? ?????????????, ??? ???????????? ????? ??? ????? ?????? ??????.

    ?? KRAKEN ???????????????? ??????????
    ? ???????????? ?????????? ? ???????????? ????????, ??? ?????? ??????? ??????? ????? ?????????????, ?????????? ?,
    ??? ?????????, ??????????
    ????? ?????????????, ??????? ??????????? ? ??????????.

  46. ?????? ???? ?????? ???? Avatar

    An outstanding share! I have just forwarded this onto a colleague who was doing a little homework on this.
    And he in fact bought me breakfast due to the fact that I stumbled upon it for him…
    lol. So let me reword this…. Thanks for the meal!! But yeah, thanks for spending some time to talk about this subject here on your site.

  47. bestonlineslot.net Avatar

    Fastidious replies in return of this matter with solid arguments and explaining everything about that.

  48. ????? ?????? ??????????? Avatar

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

Leave a Reply

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