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

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

  2. dostavka_ygEr Avatar

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

  3. dostavka_bwEr Avatar

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

  4. ?????? ?? Avatar

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

  5. dostavka_yfEr Avatar

    ???????? ???????? ?????? 24 ????
    ????????? ???????? ? ?????????, ?? ????????? ??????? ????? ???????? ????? ? ???? ?????.

  6. Surrey Moving Company Avatar

    From a studio apartment to a full family house, these movers in Surrey scale the crew to fit your move, typically two to four movers working two to ten hours depending on your inventory.
    You receive a written estimate up front, hourly billing with travel
    time included, and careful handling of furniture, boxes and specialty
    items from door to door. Packing and unpacking services save
    you days of work, and furniture is reassembled and placed exactly where you want it.
    Free quotes make it easy to plan your move with confidence.

  7. dostavka_xjEr Avatar

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

  8. dostavka_tnEr Avatar

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

  9. dostavka_ulEr Avatar

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

  10. dostavka_qdEr Avatar

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

  11. dostavka_xcEr Avatar

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

  12. dostavka_vaEr Avatar

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

  13. dostavka_ffMl Avatar

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

  14. dostavka_bwMl Avatar

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

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

  15. dostavka_foMl Avatar

    ???????? ???????? 24 ? ??????
    ???????? ???????? ????????? ???????? ??????????? ??????? ??????? ? ????? ????? ?????

  16. dostavka_hmMl Avatar

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

  17. dostavka_uhMl Avatar

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

  18. dostavka_iiMl Avatar

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

  19. dostavka_ljEr Avatar

    ???????? ???????? ?????? 24 ????
    ????????? ?????? ??????????? ??????: ?????????? ??????? ????????????? ????? ? ???????? ??????.

  20. dostavka_vnEr Avatar

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

  21. dostavka_roMl Avatar

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

  22. dostavka_kwEr Avatar

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

  23. dostavka_waMl Avatar

    ???????? ???????? 24 ????
    ????????? ???????? ?????????? ?????? ?? ???????? ???????? ???????? ? ?????????? ? ??????? ?????????

  24. dostavka_lpEr Avatar

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

  25. dostavka_pmMl Avatar

    ???????? ???????? ?????? 24
    ???????? ???????? ????????? ???????? ??????????? ??????? ??????? ? ????? ????? ?????

  26. dostavka_gcEr Avatar

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

  27. dostavka_gzMl Avatar

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

  28. dostavka_oyEr Avatar

    ???????? ???????? ?????? 24
    ???????? ?????? ???????? ??????????? ??????????? ? ???????? ??????? ??????.

  29. dostavka_ouMl Avatar

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

  30. dostavka_taEr Avatar

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

  31. dostavka_ttMl Avatar

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

  32. dostavka_nfEr Avatar

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

  33. dostavka_eqMl Avatar

    ???????? 24
    ????? ??????????? ?????? ?????????? ????????? ? ??????????? ???????? ? ??????? ?????????? ? ????????

  34. casinoonline168.com Avatar

    Oh my goodness! Awesome article dude! Thanks, However I am going through difficulties with your RSS.

    I don’t understand the reason why I cannot join it.
    Is there anyone else having similar RSS issues?
    Anyone that knows the answer will you kindly respond?

    Thanks!!

  35. ?? ? ?? Avatar

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

  36. dostavka_yaMl Avatar

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

  37. dostavka_dfEr Avatar

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

  38. Briantobia Avatar

    ?????? ?????? ????????? ????? ???? ? 2022 ???? ? ??????? ???????? https://loldentrnd.ru/

  39. dostavka_wcEr Avatar

    ???????? 24 ???? ? ??????
    ??? ?????????? ?????? ?????????? ????????? ??????, ????? ???? ???????? ?????? ????????? ???????.

  40. dostavka_brEr Avatar

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

  41. dostavka_ntEr Avatar

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

  42. dostavka_trEr Avatar

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

  43. dostavka_oqEr Avatar

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

  44. dostavka_qeEr Avatar

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

  45. dostavka_vqEr Avatar

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

  46. dostavka_kmEr Avatar

    ???????? ???????? ????? ?????? ?????????????
    ????????? ?????? ??????????? ??????: ?????????? ??????? ????????????? ????? ? ???????? ??????.

  47. dostavka_zqMl Avatar

    ???????? 24 ???? ??????
    ??????????? ????? ??????????? ???????? ???????? ? ???????? ??? ????? ? ?????, ?? ????? ???? ? ?????.

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

  48. dostavka_qqMl Avatar

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

  49. dostavka_ddMl Avatar

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

  50. dostavka_paMl Avatar

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

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

Leave a Reply

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