11

Is this possible? Has anyone tried it?

Using websockets means there are no httpheaders being exchanged (like in a Ajax request) so there is definately a boost in speed of page display, however with the sockets you are holding a connection to the server even when nothing maybe happening, is this detrimental as number of users increase?

with a connection being held between client and server, can the server still handle other clients connecting on the same port?

Chris
  • 259
  • 1
  • 4
  • 6
  • 1
    Very intriguing question. +1 One thing I might mention though is that WebSocket support, as you probably know, is still tenuous or nonexistent in a lot of clients, so compatibility will be a major factor. – Tom Nov 07 '11 at 20:26
  • Look at [socketstream](https://github.com/socketstream/socketstream). It's an entire website build on websockets. I send a tiny HTML file that opens a websocket connection, then pipes all the resources/assets down the websocket – Raynos Nov 08 '11 at 00:59
  • @Tom: actually, WebSockets can be used on pretty much all browsers now. iOS has supported it since 4.2. Chrome since 5. Safari since 5-ish. Firefox since 6.0. IE will support it in 10. Opera soon too. For all others (and older browsers) there is [web-socket-js](https://github.com/gimite/web-socket-js). Note: there are two major protocol versions Hixie-76 and HyBi-* in the wild, but most WebSocket servers support both transparently (it's relatively easy to do so) so it's not actually much of an issue. – kanaka Nov 17 '11 at 22:00

4 Answers4

4

It is definitely possible but I have not tried it. You will get a latency boost but the bandwidth boost will not be significant. The real problem is not going to be server resources (continuously polling the server via AJAX is likely harder on the server in most ways), but that AJAX has really solved a lot of the problems (especially the ones you will run into as your scope increases) so you will be rebuilding a lot of stuff for custom use.

Unless you are actually running into a latency problem, I would suggest using standard AJAX. Or only use WebSockets for the part of you application that actually needs low latency so you are not recreating all the wheels.

Yes, being able to have multiple clients connect to one listening port simultaneously is possible and done all the time (your web server almost certainly does so on port 80 for example). Your WebSocket server will have to handle the incoming connections properly (evented, threaded, or multi-process) but it's pretty much standard fair (google "socket programming YOUR_LANGUAGE").

kanaka
  • 70,845
  • 23
  • 144
  • 140
0

A late answer, but...

I've recently rolled out my amusement web-app (jounce.space) that uses a websocket for everything except authentication. The websocket is used for both server-pushed data and client-initiated transactions.

The site offers several different billiard-ball type amusements. Instead of a cue-stick, a player controls her ball with an elastic-band connecting her mouse to a ball. Unlike pool or billiards, you play continuously: you don't take turns.

The elastic band solves (or minimizes) latency problems because the player naturally expects a delay when controlling via an elastic band. The server runs the game model, sending updated ball positions and game events every 40ms via websocket. Each client machine responds with its current mouse position.

Besides channeling real-time game data, the socket also sends/recieves player arrivals & departures, game anoucements, and chat. The data are JSON formatted, e.g. every 40ms this is sent to each client:

{

    "frameN": frame_sequence_num,

    "cpuLoad": frame_update_us/frameRate,

    "players":{

        <playerId>:[posX, posY, MouseX, MouseY],

        .

        .

        .

    }

}

Sometimes the frame data arrive out of order, so if frame_sequence_num is less than the previous one, that frame data is ignored. Sending everone's mouse position to all players lets everyone see each-other's elastic-band.

Jounce.space is live as of this post. Visit and "view source" for more a more in depth look at the client side. Er, warning, the code isn't yet pretty; it's a hodge-podge of ad hocery, but it works well (except for MSIE). The server, written in golang, is hosted at openshift.redhat.com.

CoolRoar
  • 11
  • 1
  • 2
0

Build a whole site using just websockets (via socket.io and node.js, no Ajax)? Is this possible? Has anyone tried it?

I didn't hear of such a thing, but there are many websites that use Ajax intensively.

Using websockets means there are no httpheaders being exchanged (like in a Ajax request) so there is definately a boost in speed of page display, however with the sockets you are holding a connection to the server even when nothing maybe happening, is this detrimental as number of users increase?

First of all not all the browser support native websockets, just a few actually do so probably most of your users will have to do some long-pooling or something similar.

with a connection being held between client and server, can the server still handle other clients connecting on the same port?

You are right here, but I guess if you are using something like Node.js and don't have tens of thousands of users connected this won't be a problem.

Community
  • 1
  • 1
alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • 1
    Couple of notes: The web-socket-js project provides a Flash-based shim/polyfill for WebSockets which means that any Flash enabled browser can also have WebSockets. Since iOS has had WebSockets since 4.2, this effectively means WebSockets are available just about everywhere now. Second note: For small infrequent messages, a well-designed server could easily handle tens of thousands of WebSocket connections (especially compared to a server handling polled AJAX connections for thousands of simultaneous clients). – kanaka Nov 07 '11 at 21:00
  • Yes you could use flash WebSockets that's true. I think it would be best to start off without WebSockets and then integrate each portion of the app with WebSockets (for example if you have a blog first make your regular website, and then add real-time comments, after that real-time posts etc, build the app step by step). – alessioalex Nov 07 '11 at 23:21
0

Have a look at SocketStream: https://github.com/socketstream/socketstream

SocketStream is a new Node.js web framework dedicated to creating single-page real time websites.

There will be some initial loading of resources but WebSockets will be used extensively for Client <-> Server communication.

leggetter
  • 15,248
  • 1
  • 55
  • 61