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

  1. oborudovan_ijKn Avatar

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

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

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

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

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

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

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

  2. big penis capsule Avatar

    I love what you guys tend to be up too. This sort of clever work and exposure!
    Keep up the very good works guys I’ve included you
    guys to our blogroll.

  3. oborudovan_qlKn Avatar

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

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

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

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

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

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

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

  4. oborudovan_ptKn Avatar

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

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

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

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

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

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

    ?????????? ???????????? ?????????????? ??????? ????????? ? ??????????? ?????????-???.

  5. agencia seo talavera Avatar

    Somos una agencia seo talavera, dando servicio de marketing digital a empresas,
    servicios seo y geo especializado en llms

  6. oborudovan_jtKn Avatar

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

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

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

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

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

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

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

  7. oborudovan_brKn Avatar

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

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

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

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

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

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

    ?????????? ???????????? ?????????????? ??????? ????????? ? ??????????? ?????????-???.

  8. RogerNup Avatar

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

  9. helpful resources Avatar

    Hey! This post couldn’t be written any better! Reading this post reminds me of my
    old room mate! He always kept chatting about this.
    I will forward this write-up to him. Fairly certain he will have
    a good read. Thank you for sharing!

  10. breakdown Avatar

    Fascinating blog! Is your theme custom made
    or did you download it from somewhere? A theme like yours with
    a few simple adjustements would really make my blog shine.
    Please let me know where you got your theme.
    Thank you

  11. oborudovan_byKn Avatar

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

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

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

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

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

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

    ?????????? ???????????? ?????????????? ??????? ????????? ? ??????????? ?????????-???.

  12. oborudovan_zrKn Avatar

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

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

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

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

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

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

    ?????????? ???????????? ?????????????? ??????? ????????? ? ??????????? ?????????-???.

  13. oborudovan_phKn Avatar

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

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

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

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

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

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

    ?????????? ???????????? ?????????????? ??????? ????????? ? ??????????? ?????????-???.

  14. oborudovan_qiKn Avatar

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

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

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

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

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

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

    ?????????? ???????????? ?????????????? ??????? ????????? ? ??????????? ?????????-???.

  15. go here Avatar

    I always used to read post in news papers but now as I am a user
    of web thus from now I am using net for articles
    or reviews, thanks to web.

  16. scrub Avatar

    I am truly pleased to glance at this webpage posts which consists of lots of
    helpful facts, thanks for providing such data.

  17. AWS Lambda Avatar

    After exploring a few of the blog articles on your site, I
    truly like your technique of writing a blog. I saved as a favorite
    it to my bookmark site list and will be checking back soon. Please visit my
    website too and tell me what you think.

  18. usdt to btc Avatar

    It’s genuinely very difficult in this full of activity life
    to listen news on TV, thus I only use the web for that purpose, and obtain the latest information.

  19. rape nude kid Avatar

    What’s up, for all time i used to check website
    posts here in the early hours in the dawn, since i like to learn more and more.

  20. 1win bonus Avatar

    I have checked many gaming websites this week, but your website was
    especially informative. Bookmarked.

  21. dragon money ?????? ??????????? ???? Avatar

    I think this is among the most important info for me.

    And i’m glad reading your article. But want to remark on some general things, The
    web site style is wonderful, the articles is really nice :
    D. Good job, cheers

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

    Hi there to every single one, it’s genuinely a pleasant for me
    to go to see this website, it includes helpful Information.

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

    Nice blog here! Also your website loads up very fast!

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

  24. iptv planos Avatar

    Awesome! Its in fact remarkable post, I have got much clear idea concerning from this paragraph.

  25. Kopdes Merah Putih Avatar

    Helpful information. Lucky me I found your website by
    chance, and I am stunned why this twist of fate did not took place earlier!
    I bookmarked it.

  26. top 10 lawyers in karachi Avatar

    I do not even know how I ended up here, but I thought this post was great.
    I don’t know who you are but definitely you are
    going to a famous blogger if you are not already 😉 Cheers!

  27. homepage Avatar

    Thanks a bunch for sharing this with all folks you actually know what you are speaking approximately!
    Bookmarked. Please also talk over with my web site =). We will have a link exchange contract among us

  28. bokep sma viral Avatar

    Thank you a bunch for sharing this with all folks you actually realize what you
    are talking approximately! Bookmarked. Please additionally discuss with my site =).
    We can have a hyperlink change arrangement among us

  29. lô ?? Avatar

    Stunning story there. What happened after? Take care!

  30. backyard guest house builder Avatar

    Good post. I learn something totally new and challenging on sites I stumbleupon on a
    daily basis. It will always be exciting
    to read content from other writers and use a little something from their
    web sites.

  31. Application Performance Monitoring (APM) Avatar

    What’s up to all, how is the whole thing, I think every one is getting more from this site, and your views are good in support
    of new people.

  32. Women In Employment and The Challenges They Face in Society Avatar

    Howdy very cool web site!! Guy .. Beautiful .. Wonderful ..
    I’ll bookmark your blog and take the feeds also? I’m glad to find a lot of helpful information right here within the submit, we want develop more strategies on this regard, thanks for sharing.
    . . . . .

  33. MBID29269 Avatar

    Greetings from Los angeles! I’m bored to death at work so I decided to browse
    your site on my iphone during lunch break.
    I enjoy the knowledge you provide here and can’t wait to take
    a look when I get home. I’m surprised at how quick your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyways, superb site!

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

    My brother recommended I might like this website. He was
    totally right. This post truly made my day. You cann’t imagine simply how much time I had
    spent for this information! Thanks!

  35. ?????? ? ??????? ??? ???????? Avatar

    hello!,I love your writing very so much! percentage we communicate more approximately your article on AOL?
    I need a specialist on this area to unravel my problem.
    Maybe that’s you! Taking a look ahead to look you.

  36. https://cacuoc39.net/ Avatar

    Do you have a spam problem on this blog; I also am a
    blogger, and I was wanting to know your situation;
    we have created some nice procedures and we are looking to swap solutions with
    other folks, please shoot me an e-mail if interested.

  37. web site Avatar

    This is very fascinating, You are an excessively professional blogger.

    I’ve joined your rss feed and sit up for in the hunt for
    more of your fantastic post. Also, I have shared your
    web site in my social networks

  38. https://hitclub36.club/ Avatar

    Chào anh em, nhà cái HITCLUB ?ang là sân ch?i uy tín v?i giao di?n hi?n ??i.
    Kho game phong phú t? casino, khuy?n mãi h?p d?n. Mình th?y r?t ?n, ai ?ang tìm nhà cái thì nên th? nhé!
    https://hitclub36.club/

  39. best lawyers in karachi Avatar

    Hello, Neat post. There is an issue together with your web site in internet explorer,
    would test this? IE nonetheless is the market leader and a big portion of people will leave out your fantastic writing because of this problem.

  40. cá ?? tr?c tuy?n Avatar

    I’m gone to tell my little brother, that he should also visit this web site on regular basis to take updated from most
    up-to-date gossip.

  41. pokiemateau.com Avatar

    My spouse and I stumbled over here by a different web page and thought I might as well check things out.

    I like what I see so now i am following you.
    Look forward to exploring your web page again.

  42. top game bài ??i th??ng Avatar

    No matter if some one searches for his required thing, thus he/she desires to be available that in detail,
    therefore that thing is maintained over here.

  43. Check Out Your URL Avatar

    An impressive share! I have just forwarded this onto a friend
    who was doing a little research on this. And he actually ordered me dinner because I found it for him…
    lol. So let me reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending time to talk about this subject here on your site.

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

    I think the admin of this site is genuinely working hard in favor of
    his site, since here every data is quality based
    data.

  45. web site Avatar

    Hi there! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?

  46. buy business sale Avatar

    Hello! I just wanted to ask if you ever have any issues with hackers?
    My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no
    data backup. Do you have any methods to protect against hackers?

  47. slot Avatar

    Dengan mendaftar di pop108, Anda memperoleh akses penuh ke platform slot online yang menyediakan berbagai permainan slot gacor
    dengan kualitas tampilan yang baik. Anda dapat memilih
    dari banyak judul slot dengan tema berbeda, masing-masing
    dilengkapi fitur permainan yang menarik. pop108 memudahkan Anda berpindah antar permainan dengan navigasi yang lancar dan waktu muat
    yang cepat. Anda juga mendapatkan kemudahan bertransaksi serta dukungan layanan pelanggan yang siap membantu.
    Semua permainan dapat diakses melalui perangkat apa pun, sehingga Anda bisa bermain slot online sesuai kenyamanan Anda.

  48. homepage Avatar

    Hey! Do you know if they make any plugins to assist with SEO?
    I’m trying to get my blog to rank for some targeted keywords but I’m not
    seeing very good success. If you know of any please share.

    Kudos!

  49. video telanjang Avatar

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

Leave a Reply

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