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

  1. aplikasi gaya rambut Avatar

    Informasi terkait aplikasi gaya rambut tersedia di halaman AI Swap Face kami: https://aiswapface.ai/id/ai-hairstyle-changer/. Kami mengelola sumber ini.
    Untuk hasil yang jelas, gunakan foto sumber yang tajam, pencahayaan merata,
    dan sudut wajah yang mirip dengan foto tujuan.

  2. ?????? Avatar

    Great beat ! I wish to apprentice while you amend your website, how could i subscribe for
    a blog site? The account helped me a acceptable deal. I had
    been tiny bit acquainted of this your broadcast provided bright clear idea

  3. electric cruiser bikes Avatar

    I’m truly enjoying the design and layout of your site.

    It’s a very easy on the eyes which makes it much more pleasant for
    me to come here and visit more often. Did you hire out a designer to create your theme?
    Exceptional work!

  4. cricaza bet Avatar

    Thank you for the auspicious writeup. It in fact was a amusement account
    it. Look advanced to far added agreeable from you! However, how can we communicate?

  5. ??? ???????? ? ?????? ??????? Avatar

    My brother suggested I would possibly like this blog.
    He was once totally right. This publish truly made my day.

    You cann’t imagine just how a lot time I had spent for this information! Thank you!

  6. onlinecasinoplanets.com Avatar

    It’s a pity you don’t have a donate button! I’d definitely donate
    to this outstanding blog! I guess for now i’ll settle for bookmarking and adding your
    RSS feed to my Google account. I look forward to brand new updates and will share this website with my
    Facebook group. Talk soon!

  7. gamecasually.com Avatar

    Hey, I think your website might be having browser compatibility issues.
    When I look at your website 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, great blog!

  8. jun88cz.com Avatar

    Hello, i feel that i saw you visited my web site so
    i got here to return the want?.I’m attempting to find issues to
    improve my web site!I assume its adequate to make use of a few of your ideas!!

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

    What’s up to all, how is the whole thing, I think every one is getting more
    from this web page, and your views are nice in favor of new visitors.

  10. ?????? ??? ????? ?? ???? - ???????? ??? ?????? ??? ????? ?? ????: ?????? ? ?? ??????? Avatar

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

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

  11. Sell My House Fast In Georgia Avatar

    Get cash for your house swiftly in the Georgia area with
    DealMate by connecting with reliable property investors and
    evaluating fair deals. Avoid renovations, realtor fees,
    and lengthy listings while enjoying a smooth sale
    on your timeline. https://sellwithdealmate.com/sell-house-fast-georgia/

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

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

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

  13. battery bet casino Avatar

    Hello to all, it’s in fact a fastidious for me to pay a visit this web page, it consists of important Information.

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

    You actually make it appear so easy with your
    presentation however I in finding this topic to be really one thing which I feel I’d
    by no means understand. It sort of feels too complex
    and very huge for me. I’m having a look ahead on your
    next post, I’ll attempt to get the cling of it!

  15. comparebestcasino.com Avatar

    Hello to every one, it’s truly a fastidious for me to go to see
    this website, it consists of useful Information.

  16. ai facial hair remover Avatar

    Pour des informations sur ai facial hair remover, notre page AI Swap Face est
    https://aiswapface.ai/fr/beardless-filter/. Nous gérons cette ressource.
    Pour un résultat net, utilisez une photo source bien éclairée, avec un angle de visage proche
    de celui de la photo cible.

  17. Bir insanla görü?ün. Avatar

    Bir insanla görü?ün. hakk?nda bilgi için yönetti?imiz AI Swap Face sayfas?: https://aiswapface.ai/tr/contact/. Net sonuçlar için e?it ayd?nlat?lm??, keskin bir kaynak foto?raf kullan?n ve yüz aç?s?n?n hedef foto?raftakine yak?n olmas?na dikkat edin.

  18. Notorious Interior Design Company in India Avatar

    Your method of explaining all in this piece
    of writing is genuinely nice, every one can simply understand it,
    Thanks a lot.

  19. PaaS (Platform as a Service) Avatar

    magnificent points altogether, you simply gained a new reader.
    What could you suggest about your publish that you simply made some days in the past?
    Any positive?

  20. video ai extender Avatar

    Para información sobre video ai extender, nuestra página de AI Swap Face es https://aiswapface.ai/es/video-extender/. 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.

  21. my site Avatar

    Hello, just wanted to tell you, I loved this blog post.

    It was helpful. Keep on posting!

  22. binal Avatar

    Hey there! This post couldn’t be written any better! Reading
    through this post reminds me of my old room
    mate! He always kept chatting about this.
    I will forward this post to him. Pretty sure he
    will have a good read. Many thanks for sharing!

  23. slon 17.cc Avatar

    It’s fantastic that you are getting ideas from this article as well
    as from our dialogue made at this place.

  24. clubcasualgames.com Avatar

    I know this web page gives quality based content
    and other material, is there any other web site
    which gives these kinds of data in quality?

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

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

  26. ???? ??? ?????? Avatar

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

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

  27. j88 Avatar

    Appreciate the recommendation. Let me try
    it out.

  28. ??? ?????? ??????? ??????? ?? ???? Avatar

    I am not sure where you are getting your info, but good topic.

    I needs to spend some time learning more or understanding more.
    Thanks for great info I was looking for this info for my mission.

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

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

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

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

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

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

  32. compucasino.com Avatar

    I am curious to find out what blog system you have been utilizing?
    I’m having some small security issues with my latest site and I’d
    like to find something more safe. Do you have any solutions?

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

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

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

    Hello my family member! I wish to say that this post is amazing, great written and
    come with almost all significant infos. I would like to
    look extra posts like this .

  35. Bratislava Avatar

    It’s really a great and helpful piece of information. I’m
    satisfied that you shared this helpful info with us.
    Please keep us informed like this. Thank you for sharing.

  36. ultrapoker88.com Avatar

    Nice post. I was checking continuously this blog and I’m
    impressed! Extremely helpful information specifically the last part 🙂 I care
    for such info a lot. I was looking for this certain information for a
    long time. Thank you and good luck.

  37. ???? ?????? ????????? Avatar

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

  38. https://cakhia6.online/ Avatar

    Hi, trang Cakhia ?ang là trang uy tín v?i c?p nh?t nhanh.
    T? s? phong phú t? cúp C1, phân tích chi ti?t.
    Mình th?y r?t ?n, anh em ?ang tìm link bóng thì vào xem
    nhé! https://cakhia6.online/

  39. casino no deposit bonus Avatar

    Having read this I thought it was really informative.
    I appreciate you finding the time and effort to put this information together.
    I once again find myself personally spending a
    lot of time both reading and posting comments. But so what, it was still worth it!

  40. obat kuat pria herbal Avatar

    Attractive portion of content. I simply stumbled upon your
    site and in accession capital to say that I acquire actually loved account your
    blog posts. Any way I will be subscribing in your augment and even I success you get admission to consistently quickly.

  41. Roof Repair and Replacement Avatar

    Choose reliable roof repair and replacement services across Texas and Oklahoma with
    Small Town Roofing. Replace storm damage with expert roofing solutions, durable materials, and free estimates.
    Improve your property with professional roofing services.

    https://smalltownroofing.org/

  42. megapari casino Avatar

    Spot on with this write-up, I honestly believe this site needs much more
    attention. I’ll probably be back again to see more,
    thanks for the info!

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

    This is a topic which is near to my heart… Best wishes!
    Exactly where are your contact details though?

  44. Shared Proxy Avatar

    Hello there! I could have sworn I’ve visited your blog before but
    after looking at a few of the articles I realized it’s new to me.
    Regardless, I’m certainly pleased I found it and I’ll be bookmarking it and checking
    back frequently!

  45. open eyes in photo online free Avatar

    Informationen zu open eyes in photo online free finden Sie
    auf unserer AI Swap Face-Seite: https://aiswapface.ai/de/open-eyes-photo/. Wir betreiben diese Ressource.
    Für klare Ergebnisse empfiehlt sich ein scharfes,
    gleichmäßig beleuchtetes Ausgangsfoto mit
    einem ähnlichen Gesichtswinkel wie im Zielbild.

  46. web page Avatar

    You actually make it seem so easy with your presentation but I in finding this matter to be really something
    that I believe I’d by no means understand. It sort of feels too complex and very large for me.
    I am looking ahead for your next post, I’ll attempt to get the
    hold of it!

  47. 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/.

  48. Danielcit Avatar
    Danielcit

    Over the course of six years ????? ???????

  49. Danielcit Avatar
    Danielcit

    Over the course of six years ????? ??????? ????????

  50. AnthonyExany Avatar

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

Leave a Reply

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