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

  1. AirDoctor Indoor Air Quality Avatar

    What’s up, its good piece of writing about media print, we all know media is a
    fantastic source of facts.

  2. ???? ???? Avatar

    ???? ???? ?? ??? ??? ???? AI Swap Face ??? https://aiswapface.ai/ko/privacy-policy/?? ??? ? ????.
    ??? ??? ?? ??? ??? ??? ??
    ?? ??? ???? ?? ??? ?? ??? ???? ????.

  3. ?? ??? ?????? ?????? Avatar

    constantly i used to read smaller posts that as well clear their motive,
    and that is also happening with this post which
    I am reading at this place.

  4. https://exoticcarrental.com.my/luxury-car-rental-malaysia Avatar

    Hello! This is my first visit to your blog!
    We are a group of volunteers and starting a new project in a
    community in the same niche. Your blog provided us beneficial information to work on. You have done a marvellous job!

  5. web site Avatar

    What’s up, this weekend is pleasant in support of me, for the reason that this point in time i am reading this wonderful informative paragraph here
    at my home.

  6. ngentot Avatar

    Hello, I think your site might be having browser compatibility issues.
    When I look at your blog site in Firefox, 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, superb blog!

  7. JamesBut Avatar

    ??????? ???? ? ???????? ??????????? ????? Vikiland

    ??????????? ? ????????????? ??????????? ???????????, ???????????????? ? ????????????? ?????????????? ?? ???????? ???? ? ????? ? ?????????????? ?????????????? ??????? ? ??????????? ?????????????????? ???????

  8. casino en ligne Avatar

    https://www.auto-moto.com/casino-en-ligne

    Hey there, I think your site 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, wonderful blog!

  9. ??? ?????? ?????? ??????????? ???? Avatar

    I seriously love your site.. Pleasant colors & theme.
    Did you develop this website yourself? Please reply back as
    I’m trying to create my own personal blog and
    want to learn where you got this from or exactly what the theme is called.
    Kudos!

  10. Kennethmum Avatar

    ?????? ??????? ????????? ??? ???????? ???????, ?? ????????? ???? ?? ??????????: ???? ?????? ???? ?????????, ???? ??????? ???????, ? ?????? ??????????????. ???????? ?????? ??????, ?? ?????? ???? ?????? ??? ??????? ??????-?? ?????????? ??????????. ??? ??? ????? ????????? — ??? ??????? ????????? ???? ????? ? ???????? ???????????? ????????? ??? ??????? ??????????? ????? ?????????? ????? ??? ??????? ???????????? [url=https://mymoscow.forum24.ru/?1-1-0-00005004-000-0-0-1785739949] ?????????[/url]

  11. Koperasi Desa Avatar

    I am regular reader, how are you everybody? This paragraph posted at this
    web site is genuinely good.

  12. rosewood.edu.na Avatar

    References:

    Online casino sofortüberweisung deutschland rosewood.edu.na

  13. web page Avatar

    This site was… how do I say it? Relevant!! Finally I have found something
    which helped me. Thanks!

  14. Care Avatar

    Cabinet IQ
    8305 State Hwy 71 #110, Austin,
    TX 78735, Uniuted ?tates
    254-275-5536
    Care

  15. top ten business directories in usa Avatar

    Hello, i think that i saw you visited my web site so i came to “return the favor”.I’m trying to find things to
    improve my site!I suppose its ok to use some of your ideas!!

  16. ????? ?????? Avatar

    This text is worth everyone’s attention. How can I find out more?

  17. mayorcasinos.com Avatar

    My partner and I stumbled over here from a different
    web page and thought I might as well check things out.
    I like what I see so i am just following you.

    Look forward to looking over your web page repeatedly.

  18. Danielcit Avatar
    Danielcit

    We built the club for our family ?????????? ???? ? ????????????? ???????

  19. Danielcit Avatar
    Danielcit

    We built the club for our family ????? ??? ?? ??????? ?? ????????

  20. AirDoctor Best Air Purifier for Home Avatar

    Today, I went to the beachfront with my kids.

    I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her
    ear and screamed. There was a hermit crab inside
    and it pinched her ear. She never wants to go back! LoL I know
    this is entirely off topic but I had to tell someone!

  21. Largest Contentful Paint (LCP) Avatar

    Have you ever considered creating an ebook or guest authoring
    on other blogs? I have a blog based upon on the same topics you discuss and would love to have you share some stories/information. I know
    my audience would appreciate your work. If you are even remotely interested, feel free to shoot me an e-mail.

  22. diu win casino india Avatar

    I am not sure where you’re getting your info, but good topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for wonderful info I was looking for this information for my
    mission.

  23. open eyes in photo online free Avatar

    Para información sobre open eyes in photo online free, nuestra página
    de AI Swap Face es https://aiswapface.ai/es/open-eyes-photo/. Nosotros mantenemos
    este recurso. Para un resultado nítido, utiliza una foto de origen bien iluminada
    y con un ángulo de rostro parecido al de
    la imagen de destino.

  24. AirDoctor Best Air Purifier for Home Avatar

    Inspiring quest there. What happened after? Thanks!

  25. ??? ?????? ??????????? Avatar

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

  26. Michaelnaw Avatar

    Small vehicle issues can be surprising. I try to learn more about vehicle maintenance and common issues. While reading about car maintenance, I came across https://www.avtorepair.xyz . The site focuses on automotive topics, and could interest people who maintain cars. Good repair advice can save time when issues appear.

  27. ?????? ???? ??????? ???????? ??????????? ???????? Avatar

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

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

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

  28. AirDoctor Air Purifier for Allergies Avatar

    I’ve learn several good stuff here. Certainly price
    bookmarking for revisiting. I surprise how much attempt you place to make one of these great informative web site.

  29. AirDoctor Review Avatar

    Hi there, I discovered your blog via Google while searching for a similar subject,
    your site came up, it looks good. I have bookmarked it in my google bookmarks.

    Hello there, simply become alert to your weblog through Google, and found that it’s truly informative.
    I’m going to watch out for brussels. I’ll appreciate should you continue this in future.

    Lots of other people can be benefited out of your writing.
    Cheers!

  30. AirDoctor Review Avatar

    Appreciate this post. Will try it out.

  31. AirDoctor Review Avatar

    My family all the time say that I am killing my time here at net,
    but I know I am getting know-how every day by reading such good
    posts.

  32. castle apk Avatar

    What’s Going down i am new to this, I stumbled upon this I’ve found It
    absolutely useful and it has aided me out loads.

    I am hoping to give a contribution & help other customers like its helped me.

    Good job.

  33. ???? Avatar

    You can certainly see your enthusiasm in the work you write.
    The arena hopes for more passionate writers such as you who
    are not afraid to say how they believe. At all times go after your heart.

  34. casino en ligne le plus payant Avatar

    https://www.onzemondial.com/casino-en-ligne-le-plus-payant

    Fantastic goods from you, man. I have understand your stuff previous to
    and you are just too great. I really like what you have acquired here, really like
    what you’re stating and the way in which you say it. You make it enjoyable and you still take care of to keep it wise.
    I can’t wait to read much more from you. This is actually a great
    web site.

  35. mudar de roupa ia grátis Avatar

    Para informações sobre mudar de roupa ia grátis, 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.

  36. RichardSib Avatar

    ????????? 7]

    ??????????

  37. Marcuswrich Avatar

    ???????? ?????????? ????? ????????? ?????????????? ?? ?? ??????? ???????? ? ??????? ?? ??????, ?? ???????? ? ????????? ???????? ?? ??????????, ??????? ?????? ????? ??????? ????????? ?? 300 ?????? ??? ??????? ????? (?????) ?? 450 ?????? ? ??? ??????!

    ? ??????? , ?? ?????? ?????? ?????? ???? ??????? ??????????, ??? ????????????, ?????????? ?????? ??????? ? ?? ?????????, ?????????? ???????????? ????????? ???????????? ?????, ?????????? ??????? ??? ? ???????????? ????????, ??????? ??????????? ????? ????????????? ??????????, ? ??? ?? ???????? ??????? ? ??? ????????? https://zapchasti-remont.ru/shop/korpusyi_i_ramyi_ruchki/

  38. ?????? ????? Avatar

    An outstanding share! I’ve just forwarded this onto a friend
    who had been conducting a little homework on this. And he in fact ordered
    me lunch simply because I found it for him… lol. So let me reword this….
    Thanks for the meal!! But yeah, thanx for spending some time to discuss
    this subject here on your web site.

  39. ????? ?????? ??????????? ???? Avatar

    I like the helpful info you provide in your articles. I’ll bookmark your blog and check again here frequently.

    I am quite certain I will learn many new stuff right here!
    Good luck for the next!

  40. gizbo ?????? ??????????? ???? Avatar

    Thanks for another informative blog. Where else could I am getting that
    kind of information written in such a perfect method? I’ve a challenge that I’m
    just now operating on, and I’ve been on the look out for such information.

  41. non custodial crypto swap Avatar

    It’s hard to come by well-informed people about this topic, but you sound like you know what you’re talking about!
    Thanks

  42. topcasinocanada.com Avatar

    Simply wish to say your article is as astounding.
    The clarity in your submit is simply nice and i can assume you’re a professional
    in this subject. Fine together with your permission allow me to clutch your feed to keep up to date with
    impending post. Thank you one million and please carry on the gratifying work.

  43. AirDoctor Indoor Air Quality Avatar

    My partner and I stumbled over here by a different
    website and thought I might as well check things
    out. I like what I see so i am just following you.
    Look forward to finding out about your web page for a second time.

  44. web site Avatar

    Spot on with this write-up, I absolutely believe
    that this web site needs a great deal more attention. I’ll probably be
    returning to read more, thanks for the info!

  45. sweet bonanza casino online Avatar

    Hey! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly? My weblog looks weird when browsing from my iphone.
    I’m trying to find a template or plugin that might be able to correct
    this issue. If you have any recommendations, please share.
    With thanks!

  46. situs slot gacor Avatar

    Appreciate this post. Let me try it out.

  47. casino en ligne Avatar

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

    Thankfulness to my father who informed me on the topic of this
    weblog, this blog is really amazing.

  48. battery Avatar

    Hello there! This is kind of off topic but I need some guidance from an established blog.
    Is it very difficult to set up your own blog?
    I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about making my own but I’m not sure where to start.
    Do you have any tips or suggestions? Appreciate it

  49. best free app for finding businesses gor sale Avatar

    Excellent post. Keep writing such kind of information on your page.
    Im really impressed by your site.
    Hey there, You’ve done a great job. I will definitely digg it and individually suggest to my friends.

    I’m confident they will be benefited from this website.

  50. Godzilla Minus Zero ?????????? ????? ????? Avatar

    I have been browsing online more than 3 hours these days, but I by
    no means found any fascinating article like yours. It is pretty price sufficient for me.
    Personally, if all webmasters and bloggers made excellent content
    material as you did, the web will be a lot more helpful than ever before.

Leave a Reply

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