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

  1. buy a list of businesses Avatar

    Hi, this weekend is good in support of me, for the reason that this occasion i am reading
    this enormous educational post here at my home.

  2. topcasinocanada.com Avatar

    Hey! Would you mind if I share your blog with
    my facebook group? There’s a lot of folks that I think would really appreciate your content.
    Please let me know. Thanks

  3. casino-gangster.com Avatar

    Howdy! Do you use Twitter? I’d like to follow you if that would be ok.
    I’m definitely enjoying your blog and look forward to new updates.

  4. buyabusiness com Avatar

    I am actually grateful to the holder of this web site who has shared this wonderful piece of
    writing at here.

  5. banking court lawyers in karachi Avatar

    Hi there! Someone in my Myspace group shared this site with us so I came to give it a look.
    I’m definitely enjoying the information. I’m bookmarking and
    will be tweeting this to my followers! Fantastic blog and terrific design.

  6. bokep Avatar

    Pretty! This was an incredibly wonderful post. Thank you for providing this information.

  7. non profitable Avatar

    I’ve been exploring for a little bit for any high-quality articles or weblog posts in this sort of area .
    Exploring in Yahoo I ultimately stumbled upon this
    web site. Reading this information So i’m happy to exhibit that
    I’ve a very excellent uncanny feeling I discovered exactly what I needed.
    I most no doubt will make sure to don?t omit this website and give
    it a look regularly.

  8. homepage Avatar

    Inspiring story there. What occurred after? Thanks!

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

    Hello! I’m at work surfing around your blog from my new iphone 4!
    Just wanted to say I love reading through your blog and look forward to all your
    posts! Carry on the outstanding work!

  10. Deshi Sexx Avatar

    Watch free Deshi adult HD sex videos featuring
    authentic amateur content, viral MMS scandal clips, and hardcore porn. Explore 6M+ sex videos on our porn tube site, featuring
    desi hot girls, bhabhi, aunty, boudi, lovers new xxx sex videos updated daily.

  11. Check Out Your URL Avatar

    Hello there! This is kind of off topic but I need some help from an established blog.
    Is it difficult to set up your own blog? I’m not
    very techincal but I can figure things out pretty quick. I’m thinking about
    making my own but I’m not sure where to start.
    Do you have any points or suggestions? Many thanks

  12. click resources Avatar

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

  13. Google top ranking Avatar

    11. Employ the services of agencies who keep present on SEO tendencies.
    One effective technique to remain up to date on the latest traits is studying SEO news, joining webinars, and taking part
    in industry boards. Momentum Wealth’s onsite blog was
    a core part of our content strategy and has played
    a key function in attracting additional visitors to
    the location. Not solely can a fantastic content material
    strategy improve SERP rankings, but it may also assist improve the
    organic engagement and shares in your content material and site.

    Through an intensive SEO marketing campaign, we achieved outstanding results, serving to the consumer experience a
    exceptional 186% increase in natural search traffic and secure high 10 rankings for his or her goal key phrases.

    Vertical rankings embrace all forms of search results: Google Maps
    outcomes, photographs, information, video, books etc.
    Your website (or websites of your opponents) could also be ranked in any of these classes, so your prime priority is to get as lots of a lot of
    these rankings as doable and occupy as a lot space in SERPs as doable.
    We’ve fully reworked CB SEO with an growth from four to 149 Schema
    varieties! We solely use English Degree stage authors with palms on SEO experience.

  14. casino-games-guide.com Avatar

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

Leave a Reply

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