The answer to this question boils down to two things: 1) Will it be simpler to use BOSH or web sockets, given the limitations of node.js, and 2) How can I structure code so the same javascript file runs equally well in the browser as it does in Chrome (or other browser). It is hard to describe the problem succinctly.
The real problem is wanting code to run equally well in the browser as in node.js, while having external dependencies.
Background
I got this idea that I want to do small javascript projects for fun. I’m calling the idea jsFun, and the core idea is that I can spend 30 minutes to an hour making something fun and sharing it with my friends.
I started off by seeing if I could write Tetris in an hour, using nothing but Notepad++, Chrome, and Dropbox. I didn’t succeed, but it was fun.
For me, “fun” probably means a game, and it probably means multiplayer. The back-of-the-napkin sketch looks like this:
- I can make my changes from any computer and push them out through dropbox. (check!)
- I can use my dropbox public URL to serve static pages. (check!)
- The web clients can use HTML5 web sockets or BOSH to route messages through a node.js chat server.
- Game server scripts can also connect to the chat server and implement some sort of game logic.
- Game server scripts can either be running in the browser or in node.js.
Here is an excellent example of using HTML5 web sockets talking to a node.js chat server:
http://html5demos.com/web-socket
Let’s say I make multiplayer tic-tac-toe. My project needs 3 parts:
- A game client script – this is the javascript that runs in the browser and renders the game for the user.
- A chat hub script – this is a chat server that passes messages between the game clients and the game server. It runs as a node.js process.
- A game server script – this script can run in the browser for testing and debugging, or in node.js
Now, to make tic-tac-toe, I’ll make sure the chat server is running, create a the game server script and the game client script and open three web browsers – two clients and one server. At that point, I can use Chrome’s awesome debugging tools to work through any problems, make my updates in notepad++, and refresh the browsers like crazy, for 30 to 60 minutes. And maybe I have a working game at that point.
This is the complicating step:
That game server script I was running in a browser, I now want to run from node.js. In fact, I want to have the chat server monitor my dropbox server script directory for changes, and automatically run those scripts.
Node.js uses CommonJS modules, which the browser can’t load. I think I can use RequireJS and theoretically load the code in either environment, but then the problem becomes the fact that the browser and server will be using different libraries to do the web sockets — how do I make code that runs either way? Is websockets even the way to go, since it seems like it is a standard in flux, and maybe I can’t depend on the node.js websocket server to work long-term.
The only available websockets server for node.js looks like it may be a work in progress:
https://github.com/miksago/node-websocket-server
Maybe I should be using a more mature API like BOSH?
Additionally, the websocket client doesn’t come built into node.js either, so I’d have to use this:
(As a new stackoverflow user, I can’t paste the link normally. It’s https:// github.com /pgriess/node-websocket-client)
I’d have to face the challenge that my game server code, making webclient connections to the chat server, will be using different libraries in the node.js runtime environment than in the Chrome browser environment.
And maybe instead of using require.js, I could use standard javascript scripts in the browser and use node.js vm.runInContext – it looks like I could set up the global variable with similar functions before calling the scripts and it would work pretty much the same way in node.js or the browser using standard javascript code.
Restating the Question
(Assuming I’d set up the global environment beforehand in either case to provide the script with a common interface.) Is there a practical way for me to write a javascript file that accesses websocket-client-like features, that can execute in the browser or in node.js?
If it takes a bit of effort to get a web-sockets sockets running , fear not as the reward is worth far far far more than the effort.
Prior to web-sockets all truly browser based offerings have relied on the browser continuously polling the server, which is a heavy load for browser,network and server alike.
Even the simple message “hello” in a system based upon HTTP polling could result in kilobytes of data needing to be processed.
In web-sockets the amount of overhead at worst is something in the neighborhood of 15 bytes.
Another thing is that web-sockets are event driven, so your browser is free to remain idle, or doing other tasks until an actual communication event occurs.
As to your question of making code run well front end and back, it is going to be a matter of making your javascript classes modular and using the require() type functions on the server side and perhaps a simulation of that same function on the client side to inject the scripts you create.
There are many demos out there to download and tinker with , so like any new area of coding , just jump in and get your hands dirty. It will soon make sense.