Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I write javascript web socket code that will run in chrome and in node.js?
    text
    copied!<p>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.</p> <p>The real problem is wanting code to run equally well in the browser as in node.js, while having external dependencies.</p> <p><strong>Background</strong></p> <p>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.</p> <p>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.</p> <p>For me, "fun" probably means a game, and it probably means multiplayer. The back-of-the-napkin sketch looks like this:</p> <ul> <li>I can make my changes from any computer and push them out through dropbox. (check!)</li> <li>I can use my dropbox public URL to serve static pages. (check!)</li> <li>The web clients can use HTML5 web sockets or BOSH to route messages through a node.js chat server.</li> <li>Game server scripts can also connect to the chat server and implement some sort of game logic.</li> <li>Game server scripts can either be running in the browser or in node.js.</li> </ul> <p>Here is an excellent example of using HTML5 web sockets talking to a node.js chat server: <a href="http://html5demos.com/web-socket" rel="nofollow">http://html5demos.com/web-socket</a></p> <p>Let's say I make multiplayer tic-tac-toe. My project needs 3 parts:</p> <ul> <li>A game client script - this is the javascript that runs in the browser and renders the game for the user.</li> <li>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.</li> <li>A game server script - this script can run in the browser for testing and debugging, or in node.js</li> </ul> <p>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.</p> <p>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.</p> <p>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.</p> <p>The only available websockets server for node.js looks like it may be a work in progress: <a href="https://github.com/miksago/node-websocket-server" rel="nofollow">https://github.com/miksago/node-websocket-server</a></p> <p>Maybe I should be using a more mature API like BOSH?</p> <p>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)</p> <p>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.</p> <p>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.</p> <p><strong>Restating the Question</strong></p> <p>(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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload