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. Nhà cái LX88 Avatar

    Hi, LX88 hi?n là n?n t?ng uy tín v?i giao
    di?n hi?n ??i. Kho game ?a d?ng t? x? s?, khuy?n mãi h?p d?n. Tôi th?y r?t ?n, anh em ?ang tìm nhà cái thì vào
    tr?i nghi?m nhé! Nhà cái LX88

  2. Wholesale 925 silver jewelry bulk Avatar

    I am in fact thankful to the holder of this site who has shared this
    enormous paragraph at at this place.

  3. m8??? Avatar

    I have read so many posts about the blogger lovers except this piece of writing is actually a pleasant paragraph, keep it up.

  4. remont_uxet Avatar

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

  5. karkasnyy_qloa Avatar

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

  6. jasa backlink ac.id Avatar

    This paragraph will assist the internet viewers for building
    up new weblog or even a weblog from start to end.

  7. Research Avatar

    Hey there would you mind stating which blog platform you’re using?
    I’m going to start my own blog soon but I’m having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your layout seems different then most
    blogs and I’m looking for something completely unique.

    P.S My apologies for being off-topic but I had to ask!

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

    Tremendous issues here. I’m very glad to see your
    post. Thanks so much and I am having a look ahead to touch you.

    Will you please drop me a mail?

  9. XNXX Avatar

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

  10. Versatile linguistic repository Avatar

    ?ow, great story, Ive added you as a fri?nd on my Facebook!

    Feel free to visit my s?te :: Versatile linguistic repository

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

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

  12. ?????? ???? ? ?????? ??????? ??????? kraken ??????????? ???? Avatar

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

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

  13. remont_avet Avatar

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

  14. ?????? ???????? ????? Avatar

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

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

  15. egypt e visa contact number Avatar

    You actually make it seem so easy with your presentation but I find this matter to be
    actually something that I think I would never understand.
    It seems too complex and very broad for me.
    I am looking forward for your next post, I’ll try to
    get the hang of it!

  16. dreamproxies.Com Avatar

    Hey there! I know this is kind of off topic but I was wondering if you knew where I
    could get a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having difficulty finding
    one? Thanks a lot!

  17. important site Avatar

    I really like it whenever people get together and share views.
    Great site, keep it up!

  18. ???? ?? ???????????? Avatar

    Your style is really unique in comparison to other
    people I have read stuff from. Many thanks for posting when you have the opportunity, Guess I will just book mark this blog.

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

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

  20. rutor.or at Avatar

    I’ve been surfing on-line more than three hours today, yet I by no means found
    any fascinating article like yours. It’s beautiful worth enough for me.

    In my view, if all webmasters and bloggers made excellent content material as you did,
    the internet will probably be much more useful than ever before.

  21. ????????? ????? ????? Avatar

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

  22. Josephtrusy Avatar

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

  23. Portal Nombres Espana Avatar

    Me ha encantado el rigor histórico con el que se aborda el tema.
    Es muy útil contar con recursos que combinen datos estadísticos oficiales con explicaciones rigurosas sobre el origen, puedes explorar
    el origen y la evolución de cualquier nombre directamente en Portal Nombres Espana.

  24. JAV Online Avatar

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

    My web site JAV Online

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

    Hmm it looks like your site ate my first comment (it
    was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog writer but I’m still new to the whole thing.
    Do you have any tips for rookie blog writers? I’d certainly appreciate
    it.

  26. https://flavoursfromeverykitchen.de Avatar

    Hey I know this is off topic but I was wondering if you knew of any
    widgets I could add to my blog that automatically tweet
    my newest twitter updates. I’ve been looking for a
    plug-in like this for quite some time and was hoping maybe you would have some experience with
    something like this. Please let me know if you run into anything.

    I truly enjoy reading your blog and I look forward to your new
    updates.

    My web page: https://flavoursfromeverykitchen.de

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

    Hi, everything is going perfectly here and ofcourse every one is
    sharing data, that’s really fine, keep up writing.

  28. Bongdalu Avatar

    I am now not sure where you are getting your information,
    but good topic. I must spend some time learning more
    or figuring out more. Thank you for excellent information I
    used to be looking for this information for my mission.

  29. DanielFog Avatar

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

    125/100 – 50/40

    ?16
    5,7
    2

  30. ????????? ?????? Avatar

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

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

  31. abcdetest Avatar

    Thank you for sharing this informative post.
    I really enjoyed going through the information you shared here.

    Keep up the great work!

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

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

  33. castle app download apk Avatar

    Hi there! This article could not be written much better!
    Going through this article reminds me of my previous roommate!
    He continually kept talking about this. I’ll send this post to him.
    Fairly certain he’ll have a very good read.
    Thanks for sharing!

  34. Read Full Report Avatar

    Does your website have a contact page? I’m having problems locating it
    but, I’d like to shoot you an email. I’ve got some recommendations
    for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it improve over time.

  35. https://buttondown.com/dmytro-nasyrov/archive/ai-visibility-failed-checks-citation-rates/ Avatar

    This treatment of healthcare workflow integration and clinical boundaries
    avoids reducing the subject to a feature list.
    Workflow tests should cover representative records, missing information,
    conflicting inputs, permissions, review steps, and documented limitations.
    That gives readers a clearer basis for comparison. Resource: ai product development services – https://buttondown.com/dmytro-nasyrov/archive/ai-visibility-failed-checks-citation-rates/,

  36. find out Avatar

    For the reason that the admin of this website is working, no question very soon it will be well-known, due to its quality
    contents.

  37. Largest Contentful Paint (LCP) Avatar

    It’s going to be ending of mine day, except before end I am reading this
    wonderful post to improve my knowledge.

  38. Animatronic Dinosaur Rides Avatar

    Choose Animatronic Park for a varied selection of animatronic attractions, including giant dinosaur models, lifesize animal figures, realistic dinosaur costumes, oversized
    insect displays, and ride-on dinosaur products.
    The collection is designed for different themed environments, such as dinosaur parks, amusement attractions, museums, exhibitions, and
    entertainment venues. You can source an animatronic dinosaur for park decoration, select a lifesize animatronic dinosaur for a large display, or add a realistic animatronic
    animal to a wildlife-themed setting. For performances and visitor activities, realistic dinosaur costumes and adult dinosaur costumes offer additional possibilities.
    Giant Insect Models and lifesize insect models expand the available display options, while Animatronic
    Dinosaur Rides and Ride On Dinosaur products add an interactive attraction category to your
    project.

  39. webpage Avatar

    I was recommended this web site by my cousin. I am now not sure whether or not this publish is written through him as nobody else realize such exact approximately
    my trouble. You are wonderful! Thanks!

  40. vanity wallet Avatar

    I got this site from my friend who shared with me regarding this
    web page and now this time I am browsing this web page and
    reading very informative articles or reviews at this place.

  41. my website Avatar

    Generally I don’t read article on blogs, however I would like to say that this
    write-up very forced me to check out and do so!
    Your writing taste has been surprised me. Thanks,
    quite great article.

  42. kèo nhà cái Avatar

    Your method of telling the whole thing in this post is really nice,
    every one be capable of effortlessly understand it, Thanks a lot.

  43. dewa89 slot Avatar

    Very quickly this website will be famous among all blogging and site-building viewers, due to it’s nice articles

  44. 3e98cc04-55d8-f111-a05c-001dd8084cdf Avatar

    Your means of telling the whole thing in this article is in fact fastidious, all be able to simply be aware of it, Thanks a lot.

  45. hitclub Avatar

    Hey would you mind sharing which blog platform you’re
    using? I’m planning to start my own blog soon but
    I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most
    blogs and I’m looking for something unique. P.S Sorry for being off-topic
    but I had to ask!

Leave a Reply

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