Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7063715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:44:47+00:00 2026-05-28T04:44:47+00:00

The answer to this question boils down to two things: 1) Will it be

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:44:47+00:00Added an answer on May 28, 2026 at 4:44 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for me I will answer this question based on my experience. so if I'm
My question boils down to this: is it standard structure in Swing programming to
While trying to answer this question I found that the code int* p =
Also can you please answer this question? how do I get co-ordinates of selected
(Came up with this question in the course of trying to answer this other
I did some googling to try to answer this question but even after that
Thanks in Advance for reading and answer this question. I got button in asp
I have looked around on the Internet trying to answer this question. It seems
In this answer to this question , Lachlan-Hunt writes the following: With HTML5, you
In answer to this question Joel Coehoorn said Finally, only after the site's gone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.