0

I have been trying to make multiplayer applications on a website for a while. I wanted to start with a basic chat system. I made one but it is really slow. On the HTML page it send the message through AJAX to a PHP application which saves it to a text file. Then back on the HTML file, it is constantly checking the text file every 3 seconds. This is very slow and unreliable. So i looked up better ways of doing this. I found Node.js and used it along with Socket.io and express to create a faster chat application. But it only works on local host and i have no idea how to implement it on a website. So I kept looking and discovered WebSockets. Which are so confusing and seem to have very little support. I am confused how websites out there have applications that can be real time with so few options. How is this done? Am i missing a way of doing this? If you can help me that would be great.

  • just an fyi, socket.io is using websockets, and falls back to long polling. – Kevin B Aug 11 '15 at 22:00
  • More alternatives are server sent events, and long polling – Kevin B Aug 11 '15 at 22:01
  • Can you explain more what those are? – TYLER TRACY.17 Aug 11 '15 at 22:03
  • No, but a quick google search might. – Kevin B Aug 11 '15 at 22:04
  • a lot of real time applications use something like a queueing service to act as a intermediary for the application and the database. A chat system like what your suggesting however might benefit from a realtime database like rethinkDB. – Chad Watkins Aug 11 '15 at 22:05
  • Long polling seems pretty much the same as Websockets. Which works with JavaScript. But doesn't seem to have much support with PHP. Sever sent events seem promising though, so does rethinkdb – TYLER TRACY.17 Aug 11 '15 at 22:15
  • 1
    Long polling is what websockets are meant to replace and websockets are superior in every way. http://stackoverflow.com/questions/10028770/html5-websocket-vs-long-polling-vs-ajax-vs-webrtc-vs-server-sent-events – erapert Aug 11 '15 at 22:18

2 Answers2

-1
  1. Socket.io is using websockets under the hood already. Using raw websockets isn't necessary for your chat app.
  2. You're on the right track using socket.io and node.js server-side.
  3. Building a multi-player game in the browser will be a very difficult task for a beginner. But that's why it's good to learn! I suggest using a library for graphics (a quick google gave me this: http://www.pixijs.com/ ).

The over all architecture should be something like:

  1. Users go to your server and receive a web page (.html) which contains the javascript and a canvas they need to play the game. This is the "client-side" because it's all running on each users' web browser on their computer.
  2. The web page runs the javascript which talks to the node.js server using socket.io. This is the "server-side". The job of the server is to coordinate player data (who is who, where are they in the game, who's doing what etc) and keep track of the game state. Basically, this is where the game actually is, kind of like having the Monopoly board on the server, while the client-side is really just responsible for showing the board to the players (drawing it on the HTML5 canvas) and sending player input to the server.

Tutorials:

  • USE GOOGLE. Try just literally searching for "javascript game tutorial". Try every tutorial that comes up. If something is taking a while to get up and running then ditch it and move to another one.
  • Do simple little things at first 'till you start to grok what the overall process is like. For example: https://developer.mozilla.org/en-US/docs/Games/Workflows/2D_Breakout_game_pure_JavaScript
  • Remember playing ultra-simple games like Pong? Try writing games like this first. Your chat system is a great start, by the way, because it covers the basics of how to get a server up and running, how to get a page, how to send data around.

As for getting something up and running on a server where others can connect to it... Check this out: have your buddies come to your house and bring their laptops, start your node.js chat server, tell them what your IP address is, and have them go to "http://YOURIPADDRESS:8000" in their browser-- they'll connect to your node.js server!

Getting it running on a hosting provider is a little more involved and probably not worth the trouble at this stage. You'll learn more about this later just by keeping on the way you're going.

erapert
  • 1,383
  • 11
  • 15
-1

Socket.IO does not only work on localhost. You will need to get a server to run your application on. I highly recommend not worrying about this piece of the puzzle just yet, as it is somewhat complicated if you are brand new at this. Come back to this part when you are ready.

As for the game development, I recommend using Phaser. It has everything you need to get started and great documentation.

http://phaser.io

mattpker
  • 119
  • 5