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. ??? ?????? ?????? Avatar

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

  2. Trusted Article Link Avatar

    Thanks for some other excellent article. Where else may anybody
    get that kind of info in such an ideal means of writing?
    I’ve a presentation subsequent week, and I am on the look
    for such information.

  3. comment-3094051 Avatar

    It’s amazing in favor of me to have a site, which is useful in favor of my
    knowledge. thanks admin

  4. helpful hints Avatar

    If you are going for best contents like I do, simply
    visit this web site everyday since it offers quality
    contents, thanks

  5. ???????? Avatar

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

    ??????? ???????????? ??????????? ????????
    ????? ??????? ??????????? ????????? ????? ????????? ????? ???????????
    ????????.

  6. Trouver Mon Assurance Avatar

    Nous presentons Trouver Mon Assurance, un site en francais sur les assurances du quotidien. Decouvrez nos publications : https://trouver-mon-assurance.com/.

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

    Hi there! This article couldn’t be written any better!

    Looking at this post reminds me of my previous roommate!
    He always kept preaching about this. I most certainly will forward this article to him.
    Pretty sure he’s going to have a great read. Thank you for sharing!

  8. Kraken ??????? ??????? 2026 ?? ?????? ???? ????????? ??????? ??? Avatar

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

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

  9. slot gacor Avatar

    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 on. Cheers

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

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

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

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

  12. go here Avatar

    Heya i am for the first time here. I came across this board
    and I find It really useful & it helped me out a lot.

    I hope to give something back and help others like you aided
    me.

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

    It’s the best time to make a few plans for the future and
    it’s time to be happy. I have learn this post and if I could
    I desire to suggest you few interesting issues
    or suggestions. Perhaps you could write subsequent articles regarding this article.
    I want to learn even more issues about it!

  14. casino en ligne Avatar

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

    Hi, I do think your website could possibly be
    having web browser compatibility issues. When I look at
    your web site in Safari, it looks fine but when opening in IE, it’s got some overlapping issues.
    I just wanted to provide you with a quick heads up!
    Other than that, great blog!

  15. ?????? ????????? ?????? Avatar

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

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

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

  17. kush casino Avatar

    Excellent article. Keep posting such kind of info
    on your page. Im really impressed by it.
    Hello there, You’ve performed a fantastic job. I
    will certainly digg it and in my opinion recommend to my friends.
    I am sure they will be benefited from this web site.

  18. Tips Analisa Harga Emas Avatar

    Keep on working, great job!

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

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

  20. remont_ezSi Avatar

    ???? ??? ????????? ??????????? ? ???????? ?????? ??????? ????????, ??????????? ? ????? ????????????, ??????? ?????? ????????? ??? ?????? ?????? ? ???????????.
    ???????????? ?????? ??????? — ????? ?????? ? ???????? ?????? ??????? ?????????????. ? ??????? ???????????? ??????? ?????????? ????????????? ? ?????????? ????????? ??????????????. ??? ??????? ? ??????? ????????????, ???????????? ???????? ??? ??????????? ????????????? ??????????????. ??? ??????? ???????? ???????? ?????? ??????? ? ????????. ????? ??????????? ???????? ????????????? ???????????? ???????????? ? ??????? ????????. ????????, ??? ??????????? ???????? ?????????? ?? ??????? ??? ??????. ???? ???????? ??? ?????????? ???????? ????????????, ????????? ??? ?????? ??? ?????? ??????. ?????????????? ????????????? ? ?????????? ????? — ????? ?????? ????? ???????.

    ?????????????? ???? — ???????? ????????????????? ??????? ? ???????? ????. ? ???????? ???????? ??????????? ??????? ????, ???????? ? ???????? ????. ?????? ????? ??????? ???????????? ????? ??????? ?????? ???????? ??????????????? ? ?????????? ??? ?????????????. ?????????? ???????????? ? ????????????? ?????? ???????? ???????? ???? ?????? ???????????? ? ?????????? ???????????? ?????? ????.

  21. R777 Avatar

    Fine way of telling, and nice paragraph to take data regarding
    my presentation subject, which i am going to present in school.

  22. a href backlink Avatar

    Hi to all, the contents present at this web site are truly amazing for people experience, well,
    keep up the good work fellows.

  23. ???????? ?????? ? ?????? Avatar

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

  24. ????????? ???????? Avatar

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

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

  25. Konburt Avatar

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

  26. remont_ddSi Avatar

    ???? ??? ????????? ??????????? ? ???????? ???????? ?? ??????? ???????, ??????????? ? ????? ????????????, ??????? ?????? ????????? ??? ?????? ?????? ? ???????????.
    ?????? ??????? — ?????? ???? ? ??????????? ??????????? ?????????????. ? ??????? ???????????? ??????? ?????????? ????????????? ? ?????????? ????????? ??????????????. ?????? ??? ? ??????? — ????????? ???????????? ???????? ? ??????????? ?????????? ????. ??? ??????? ???????? ???????? ?????? ??????? ? ????????. ????????????? ???????????? ???????????? ??????????? ???????? ??????? ??????????? ? ???????? ??? ????????. ????????, ??? ??????????? ???????? ?????????? ?? ??????? ??? ??????. ????? ???????????? ????? ?? ?????, ??? ???? ???????????, ???? ???????? ?? ?????. ?????????????? ????????????? ? ?????????? ????? — ????? ?????? ????? ???????.

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

  27. Get More Info Avatar

    May I simply say what a comfort to uncover someone who genuinely knows what they’re talking about on the internet.
    You certainly know how to bring a problem to light and make it important.
    More people should read this and understand this side of your story.
    I was surprised that you aren’t more popular since you certainly possess the gift.

  28. TK 666 Avatar

    Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you!
    By the way, how could we communicate?

  29. ????? ??? ?????? Avatar

    When I originally left a comment I appear to have clicked on the -Notify me when new comments are added- checkbox and from now
    on each time a comment is added I recieve four emails with
    the same comment. Perhaps there is a means you can remove me
    from that service? Thanks!

  30. more Avatar

    Greetings! I know this is somewhat off topic but I was wondering
    if you knew where I could locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having problems
    finding one? Thanks a lot!

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

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

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

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

    These are really great ideas in about blogging. You have touched some nice points here.
    Any way keep up wrinting.

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

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

    ?????????? ???????????? ???????????? ??????????? ????? ??????? ??????????? ????????? ????? ?????????
    ????????.

  34. link Avatar

    We’re a group of volunteers and starting a new
    scheme in our community. Your web site provided us with valuable information to work
    on. You have done an impressive job and our whole community will be grateful to you.

  35. ???? Avatar

    Yes! Finally someone writes about Web3 gaming token.

  36. wow boosting Avatar

    Thanks for your marvelous posting! I seriously enjoyed reading it, you might
    be a great author.I will remember to bookmark your blog and will eventually come back from
    now on. I want to encourage one to continue your great work, have a nice weekend!

    Feel free to surf to my web site :: wow boosting

  37. here Avatar

    It’s perfect time to make a few plans for the longer term and it’s time to be
    happy. I’ve learn this post and if I may just I wish to suggest
    you some fascinating things or suggestions. Perhaps you can write subsequent
    articles relating to this article. I wish to read more things approximately it!

  38. Danielgag Avatar

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

    ?????????????? ???????????? (? ????????? ?????? ? ????? ???????????) https://gorizonthostel.ru/info.html

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

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

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

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

  40. Cesar Avatar

    Yet such a sight covers the method which such skills establish over childhood.

  41. ???????? ????????? Avatar

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

  42. ?????? ?????? ??????????? ???? ??????? Avatar

    Hi there to all, it’s truly a nice for me to
    visit this web site, it contains priceless Information.

  43. Vercel Postgres Avatar

    Valuable information. Lucky me I discovered your site accidentally, and
    I’m surprised why this coincidence didn’t came about in advance!
    I bookmarked it.

  44. 23win.directory Avatar

    Hi, 23WIN là c?ng game uy tín v?i giao di?n hi?n ??i.
    S?nh game ?a d?ng t? th? thao, khuy?n mãi h?p d?n.
    Mình ?ang ch?i ?n ??nh, ai ?ang tìm nhà cái thì nên th? nhé!
    23win.directory

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

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

    ?????????? ???????????? ???????????? ??????????? ?? ??????? ???????????? ????????? ????? ????????? ????? ????????? ????????.

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

    It’s awesome designed for me to have a web site, which is good in support
    of my know-how. thanks admin

  47. Link truy c?p CWIN05 Avatar

    Xin chào, cwin ?ang là n?n t?ng ch?t
    l??ng v?i giao di?n hi?n ??i. S?nh game phong phú t? game bài,
    khuy?n mãi h?p d?n. Mình c?m th?y r?t ?n, m?i ng??i ?ang tìm nhà cái thì
    nên th? nhé! Link truy c?p CWIN05

  48. Best Private Proxies Avatar

    I am genuinely pleased to glance at this website posts which contains tons of useful facts, thanks for providing these data.

Leave a Reply

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