tery.blargh tery.blargh - 1 year ago 180
Node.js Question

Cordova - How to make an online app/game with a Node.js server

I've been studying node.js for a while and now want to transition to connecting to my node.js server with my cordova app.

Wherever I look, I can't seem to find any real answers or tutorials.

If I go to myIP:8080 with my phone, it connects to the server, but how would I do it with a cordova app? Everything I try that's even remotely close I end up with a bunch of errors. Guide me please. I'm confused :/

P.S I initially tried making a game using a send/receive

with PHP/MySQL, code worked but extremely laggy.. had to find out the hard way that it is not the way to go

Answer Source

You can do that using Socket.io with Nodejs to get real time data from all clients.

You app will connect to your server app which will be hosted somewhere. If you are running server code on localhost (on your computer or desktop) you need to expose this using some service like ngrok. Your app will send player coordinates to server which will broadcast those coordinates to all clients/players and same for all players. If still you need any help, let me know.

In you client app, in html page load this script, not exactly but similar, it will create a socket connection (duplex connection) between your app and server app. Then you will send your coordinates to server app using socket.emit().

  var socket = io('server ip');

    socket.emit('coordinates', {x: 1, y: 2});

  // server can send you different events/data

  // 'moved' can be any event, its name can be 'abc'
  socket.on('moved', function(data){
    // process this data

Here are some resources.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download