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

    I have read so many articles regarding the blogger lovers but this post is really a pleasant post, keep
    it up.

  2. remont_yiet Avatar

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

  3. bokep 18+ Avatar

    I’m gone to inform my little brother, that he should also pay a visit this webpage on regular
    basis to obtain updated from latest reports.

  4. remont_ybet Avatar

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

  5. remont_gdet Avatar

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

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

  6. remont_enet Avatar

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

  7. remont_opet Avatar

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

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

  8. remont_eiet Avatar

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

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

  9. karkasnyy_croa Avatar

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

  10. ai face swap batch Avatar

    Pour des informations sur ai face swap batch, notre
    page AI Swap Face est https://aiswapface.ai/fr/batch-face-swap/. 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.

  11. remont_ywet Avatar

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

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

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

  12. remont_tcet Avatar

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

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

  14. ai skin tone changer Avatar

    ai skin tone changer hakk?nda bilgi için yönetti?imiz AI Swap Face sayfas?: https://aiswapface.ai/tr/face-editor/. 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.

  15. https://careers.learnbuildandgrow.de Avatar

    Hello to every one, the contents present at this web page are genuinely awesome for people
    experience, well, keep up the good work fellows.

    Feel free to visit my webpage – https://careers.learnbuildandgrow.de

  16. remont_uret Avatar

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

  17. karkasnyy_wuoa Avatar

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

  18. karkasnyy_xfoa Avatar

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

  19. karkasnyy_eooa Avatar

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

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

  20. karkasnyy_rioa Avatar

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

  21. karkasnyy_lqoa Avatar

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

  22. karkasnyy_dcoa Avatar

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

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

  23. remont_acet Avatar

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

  24. remont_vyet Avatar

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

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

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

  25. remont_aqet Avatar

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

  26. remont_roet Avatar

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

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

  27. remont_ioet Avatar

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

  28. remont_xget Avatar

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

  29. karkasnyy_onoa Avatar

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

  30. remont_spet Avatar

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

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

  31. karkasnyy_inoa Avatar

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

  32. remont_qeet Avatar

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

  33. remont_buet Avatar

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

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

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

  34. karkasnyy_aloa Avatar

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

  35. remont_qbet Avatar

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

  36. remont_cjet Avatar

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

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

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

  38. karkasnyy_vxoa Avatar

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

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

  39. remont_xaet Avatar

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

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

    Have you ever thought about adding a little bit more than just your articles?
    I mean, what you say is important and all. However imagine if you added some great pictures or videos
    to give your posts more, “pop”! Your content is excellent but with images and clips, this blog could undeniably be one of the best in its field.
    Very good blog!

  41. Kennethmum Avatar

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

  42. remont_obet Avatar

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

  43. karkasnyy_gyoa Avatar

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

  44. remont_hvet Avatar

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

  45. site Avatar

    Hey! Do you know if they make any plugins to safeguard against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on.
    Any tips?

  46. karkasnyy_deoa Avatar

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

  47. Parapsychology Avatar

    Hi, i read your blog occasionally and i own a similar one and i was just wondering if
    you get a lot of spam responses? If so how do you prevent it, any plugin or anything you can suggest?

    I get so much lately it’s driving me crazy
    so any help is very much appreciated.

  48. karkasnyy_ploa Avatar

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

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

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

  49. karkasnyy_asoa Avatar

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

  50. casinonightstexas.com Avatar

    I am really loving the theme/design of your website.
    Do you ever run into any browser compatibility issues? A small number of my blog audience have complained about my website
    not working correctly in Explorer but looks great in Chrome.
    Do you have any ideas to help fix this issue?

Leave a Reply

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