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

  1. t? l? kèo tr?c tuy?n Avatar

    Very good site you have here but I was wondering if you knew of any user discussion forums that cover the same topics discussed here?
    I’d really love to be a part of online community
    where I can get opinions from other knowledgeable individuals that share the
    same interest. If you have any recommendations,
    please let me know. Appreciate it!

  2. Terms of Service Avatar

    For related information about Terms of Service, our AI Swap Face page is https://aiswapface.ai/terms/. We
    maintain that resource. For clear face-swap results,
    use a sharp source image with even lighting and a face angle close to the target
    image.

  3. nauka p?ywania dla niemowl?t i dzieci ko?obrzeg Avatar

    He ist dieses ein großer Pfosten. Kann ich einen Teil auf ihm auf meinem Aufstellungsort benutzen?
    Ich würde selbstverständlich mit Ihrem Aufstellungsort verbinden, also konnten Leute
    den vollen Artikel lesen, wenn sie zu wünschten. Dankt jeder
    Weise.

  4. klia limo Avatar

    I was wondering if you ever thought of changing the structure
    of your website? Its very well written; I love what youve got to say.

    But maybe you could a little more in the way of
    content so people could connect with it better.
    Youve got an awful lot of text for only having one or two images.
    Maybe you could space it out better?

  5. individual Avatar

    If you are going for finest contents like me, simply pay a visit
    this site daily as it provides feature contents, thanks

  6. Discrete Rehab And Detox Phoenix Avatar

    Undeniably believe that that you stated. Your favorite reason seemed to be at the internet the simplest factor to consider of.
    I say to you, I certainly get annoyed at the same time as other people
    consider worries that they just do not understand about.
    You managed to hit the nail upon the highest and also defined out the whole thing
    with no need side effect , people could take a signal.
    Will likely be back to get more. Thanks

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

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

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

  8. Return & Refund Policy process explained Avatar

    Hi it’s me, I am also visiting this web site regularly, this
    site is actually nice and the users are in fact sharing fastidious thoughts.

  9. Compounding Pharmacy Avatar

    I’ll right away snatch your rss feed as I can’t to find your email subscription hyperlink
    or newsletter service. Do you’ve any? Kindly permit me know
    so that I may subscribe. Thanks.

  10. air purifier filter Avatar

    Does your blog have a contact page? I’m having problems locating it but, I’d like to send
    you an e-mail. I’ve got some suggestions for your blog you might be interested in hearing.

    Either way, great website and I look forward to seeing it develop over
    time.

  11. Peptide Suppliers Avatar

    Good post. I learn something totally new and challenging on sites
    I stumbleupon everyday. It will always be useful to read through content from other writers and use
    something from other websites.

  12. Canadian wildfires Avatar

    Wow, this post is nice, my sister is analyzing these things, therefore I am going to convey her.

  13. ????? Avatar

    You’ve made some good points there. I looked on the internet for more information about the issue
    and found most people will go along with your views on this web site.

    Feel free to surf to my site :: ?????

  14. Edwardedurf Avatar

    ?????? ? ????????? ????? ????????? ??????? ???????? ??? ??, ?? ????? ????????????? ???????? ??????. ??????????? ???????? ??????? ?????????????, ?? ????????? ????????? ????? ? ??????? ?????????? ?????????. ? ????? ?????? ???????? ??????? ?????????? ???????????? ??????? ? ????????? ??????????. ??? ?????????? ???????????? ??????? ???????? ??? ??? ????? ??????? ?????? ??????????????? ? ????????? ?????? ?? ??????? ???????? ?? ????????? ?????????? ??? ??????? ? ??? ????? ??????? ??? ??

  15. bath towels dubai Avatar

    Hey there! Someone in my Facebook group shared this site with us so I came to check it out.

    I’m definitely loving the information. I’m book-marking and will be tweeting
    this to my followers! Excellent blog and great design.

  16. Types Of Dowers in Nikkah Avatar

    I am now not certain the place you are getting your information, however
    great topic. I must spend some time studying more or figuring out more.

    Thank you for fantastic info I used to be in search of
    this information for my mission.

  17. Glory Avatar

    Saved this article because the topic is relevant.

  18. site here Avatar

    I just like the helpful info you supply on your articles.
    I’ll bookmark your weblog and test once more right here frequently.

    I am quite sure I’ll learn many new stuff proper here!
    Good luck for the following!

  19. MBOCentre Global Updates Avatar

    I like looking through an article that will make men and women think.
    Also, many thanks for allowing for me to comment!

  20. check my site Avatar

    Undeniably believe that which you stated.
    Your favorite reason appeared to be on the web
    the simplest thing to be aware of. I say to you, I definitely get annoyed while people think about worries that they plainly do not know about.
    You managed to hit the nail upon the top and also defined out the whole thing without
    having side effect , people can take a signal. Will likely be
    back to get more. Thanks

  21. t? l? cá c??c bóng ?á Avatar

    Hi I am so thrilled I found your blog, I really found you by accident,
    while I was looking on Digg for something else, Regardless
    I am here now and would just like to say thanks for a marvelous post and a all round interesting 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
    a great deal more, Please do keep up the awesome work.

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

    I was recommended this website by my cousin. I’m not
    sure whether this post is written by him as nobody else know such detailed about my trouble.
    You are amazing! Thanks!

  23. AWS Lambda Avatar

    I was more than happy to discover this web site.
    I need to to thank you for your time due to this fantastic read!!
    I definitely appreciated every bit of it and i also have you book-marked to see new information on your blog.

  24. Danielcit Avatar
    Danielcit

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

  25. this bonuses read Avatar

    Hi, i believe that i saw you visited my weblog so i came to go
    back the choose?.I am trying to to find things to enhance
    my site!I suppose its ok to use a few of your concepts!!

    Here is my homepage – this bonuses read

  26. Briantobia Avatar

    ???????? ????? ?2 ?? ??????? ???????? ?? AFIDENT https://loldentrnd.ru/paradotology

  27. Peter donge Avatar

    For a long time I have been researching numismatics and antiques. I love discovering the history behind historic coins and valuable antiques.
    While searching online I discovered https://groshi.xyz . The website looked very promising. There was an impressive amount of useful content about rare collectibles.
    One thing I really appreciated was the quality of the information. Although the project is not fully launched yet, it already looks very promising.
    I heard that the project will be fully available soon. I’ll definitely come back, because I believe there will be many more expert information for antique enthusiasts.
    If you enjoy numismatics or historical artifacts, I think checking out this site. I think it will be very useful once it officially opens.

  28. ?????? Avatar

    I know this site offers quality based posts and extra stuff,
    is there any other web site which provides such information in quality?

  29. ???? ?? Avatar

    ????????????????????????????????????????????????????????????????????????????????????????

    ?????????????????????????????????????????????????????????????????????????????????????????????????

    ????????????????????????????????????????????????????????????????????????????????????

    ???????????????????????????????????????????????????????????????????????????????????????
    ??????????????????????????????????????????????????????????????????????????????????????

  30. Website Avatar

    It’s remarkable to visit this web page and reading the views
    of all mates concerning this paragraph, while
    I am also zealous of getting experience.

  31. http://poeskovek.flybb.ru/viewtopic.php?f=2&t=1111 Avatar

    With havin so much content do you ever run into any issues of
    plagorism or copyright infringement? My blog 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 authorization. Do you know
    any techniques to help prevent content from being stolen? I’d
    truly appreciate it.

  32. Danielcit Avatar
    Danielcit

    ??????? ??????? ???????? ????, ????????? ????? ? ???????? https://rejting-stroitelnyh-kompanij-domov-iz-gazobetona.ru/

  33. VistaChangeBot P2P ????? ?? ???????? Avatar

    What’s up, I would like to subscribe for this blog to obtain hottest updates, thus where can i do it please help.

  34. porno sitesi Avatar

    I am sure this paragraph has touched all the internet viewers, its
    really really nice paragraph on building up new webpage.

  35. Private Proxy List Avatar

    I’m impressed, I must say. Seldom do I encounter a blog that’s equally educative and amusing,
    and without a doubt, you have hit the nail on the head.
    The issue is something that too few folks are speaking intelligently about.
    I am very happy I came across this in my search for something concerning this.

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

    ????????? — ??? ????????, ???? ??????? ??????? ?? ???????????
    ??????????? ???????????? ? ??? ?????????? ??????????, ??????
    ?????? ??????? ?????? ????????.
    ?????? ?????????? «??? ????????», «???????? ?????-??????» ?? ????
    ???????? ??????-???????, ?????????, ??? ?????? ????????????
    ? ????? ??????. ???? ?????????? ????????? ?????????? ? ??????? ???, ??? ???????? ???????????
    ?????????, ???-??? ????? ?????
    ????? ??????????. ?????? ?????? ?? ???? ?????? ????????? ????????: ????????
    ????????? ?????, ??????, ????????? ????, ????? ?? ??????? ???????.

  37. useful content Avatar

    Have you ever thought about adding a little bit more than just your articles?
    I mean, what you say is fundamental and all. But think about if you added some great
    images or video clips to give your posts more, “pop”!
    Your content is excellent but with pics and clips, this site could definitely be one of
    the greatest in its niche. Very good blog!

  38. z-library Avatar

    An impressive share! I’ve just forwarded this onto a friend who was conducting a little homework on this.

    And he actually bought me lunch due to the fact that I found
    it for him… lol. So allow me to reword this…. Thanks for the
    meal!! But yeah, thanks for spending some time to talk
    about this matter here on your web page.

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

    «?????? ?????????? ???? ????????????»: ????????-???????? ?? ??????????
    ???? ???? ??? ???? ??????? ??????? ??????? ??????? ???????? ??????, ??????? ??????????? ? ????????????.
    ?? ???????? ????? ?????????? ? ??????????? ?????? ??? ???????? ???, ?????? ??? ??????, ??? ??? ??????
    ?????????? ??????? ??????????? ???? ????? ? ??? ???????????? ??????? ?? ??????? ???????????.

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

    ??????. ????????? ??????? ??????
    ????????, ????????????? ?? ??????????.
    ?????? ?????? ????? ???????????, ??? ???????? ??? ?????????,
    ????????? ? ??? ??????? ?????????? ????? ???? ? ??????? ???? ?????.
    ?????????? ??????????? ??????????? ???????? ?? ?????? ??????????? ????????? ?????
    ????????? ????? ??????????? ????????.

  41. u888 Avatar

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

  42. bokep anak kecil Avatar

    Appreciation to my father who told me concerning this blog, this blog
    is truly amazing.

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

    ????????? — ??? ????????, ???? ????????? ??????? ?????? ???????????
    ??????????? ???????????? ????? ?????????? ???????????????,
    ?????? ? ????????? ????????. ????? ?????? ?????????? «???
    ?????????», «?? ????????» ?? ???? ????? ??????, ???????, ??? ????? ?????????? ? ??????? ????? ??????.
    ???? ????????? ???????????? ?????????? ?? ???, ??? ??????????? ??????????? ?????????, ???-??? ????????? ????? ????? ??????????.
    ???????? ?? ?????? ????????: ??????? ?????? ????????, ??????, ???????,
    ????? ????? ??????? ???????.

  44. ????????? ????? ????? Avatar

    ??????. ????????? ???????? ?????? ????????, ????????????? (?) ????? ??????????.

    ???????? ????? ??????????? ?????, ??? ???????? ????? ?????????, ?????????
    ? ??????? ????????? ????? ????
    ???? ???????? ???? ?????. ??????? ??????????? ???????????? ???????? (?) ????? ???????
    ???????????? ????????? ????? ??????????? ????????.

  45. casinoonline168.com Avatar

    What’s up it’s me, I am also visiting this site regularly, this web page is truly nice and the people are really sharing good thoughts.

  46. xx8 Avatar

    This is very interesting, You’re a very skilled blogger.
    I have joined your feed and look forward to seeking more
    of your fantastic post. Also, I have shared your web site in my social networks!

  47. ???????? ???????? Avatar

    «?????? ?????????? ?? ??????»: ???????? ? ??????????
    ???? ???????? ????????????? ???? ???????
    ??????? ??????? ?????????, ?? ??????????? ? ????????????.
    ?? ?? ? ????? ???????????? ? ?????????? ????
    ??????????? ?????? ??? ???????? ????,
    ?????? ???? ?????????, ????? ????????? ?????? ???????????
    ???? ????? ???? ????????? ? ???
    ?? ??????? ?? ??????? ???????????.

  48. more helpful hints Avatar

    Hello there, I think your blog could be having web browser
    compatibility problems. When I look at your
    website in Safari, it looks fine but when opening in I.E., it has some overlapping issues.
    I just wanted to provide you with a quick heads up!
    Besides that, excellent website!

  49. Read Full Article Avatar

    I love your blog.. very nice colors & theme. Did you design this website
    yourself or did you hire someone to do it for you? Plz reply as I’m looking to design my own blog and would like
    to find out where u got this from. thanks a lot

Leave a Reply

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