-3

The whole point is to understand how computers should communicate with each other using javascript (no jQuery framework) and ajax (by using XMLHttpRequest). I have a memory game and I would like to add an online option for people so they can play with their friends. I was thinking about a peer 2 peer connection, but I didn't know how exactly and that is why I posted this question (I am sorry if my question is not meant to be here). This is what I had in mind:

User 1: 1. click->sendAction to server

Server: 1. receiveInfo->saveDetailsInDB 2. packReceivedInfo->send to User 2

User 2: 1. receiveInfo->updateDetails

Edit: Thanks to fstanis, I now know that I should use a WebSocket method in order to communicate users/computers with each other.

Aerial
  • 1,185
  • 4
  • 20
  • 42
  • Google `XMLHttpRequest` – Pointy Sep 22 '14 at 18:37
  • There's a lot of missing information here. For one thing, we don't know if you want peer to peer, or client/server communication. – Robert Harvey Sep 22 '14 at 18:38
  • I did that. I know how to use ajax, but how can I try to communicate asynchronously? What method should I know? Robert, it needs to be peer to peer connection. – Aerial Sep 22 '14 at 18:38
  • Are you trying to build a Chat program? – Robert Harvey Sep 22 '14 at 18:39
  • No, a game where people can play online together. – Aerial Sep 22 '14 at 18:39
  • AJAX **is** async. Are you asking for WebRTC? – SLaks Sep 22 '14 at 18:40
  • Everyone knows that SLaks, I just want to know which method there is in order to communicate 2 computers. I want to receive message when the other player kliks a button, something like that... – Aerial Sep 22 '14 at 18:41
  • @Xarialon The downvotes weren't well-explained: this is too broad of a question for SO, and falls into the "recommend a product/solution to me" category of questions, which are typically not on-topic for Stack Exchange sites. As to your question, [socket.io](http://socket.io/) ([Wikipedia page](https://en.wikipedia.org/wiki/Socket.IO)) is an implementation library that provides JavaScript-based communication using a variety of technologies (primarily [WebSocket](https://en.wikipedia.org/wiki/WebSocket) technology). You may also benefit from [this article](http://bit.ly/1x1WW8D). – ajp15243 Sep 22 '14 at 18:48
  • @ajp15243 That is because you don't have to know the whole idea. I just wanted to know what a programmer should do in order to communicate 2 users. You don't need to know if it's a chat/game website. Thank you for explaining it, I will try to understand how a WebSocket works. – Aerial Sep 22 '14 at 18:58
  • @Xarialon You're welcome, I think WebSockets are probably the answer you need. Regarding not having to know "the whole idea", that's part of what makes questions like this incompatible with Stack Exchange sites. They are Q&A sites, and necessitate specific questions by design. Often, one can give the right level of details on the [correct SE site](https://stackexchange.com/sites) and the question will end up being on-topic. A pitfall is giving *too* much detail, which makes the question too localized and unlikely to help others (I mention this only for completion's sake). – ajp15243 Sep 22 '14 at 19:03
  • @ajp15243 I changed the whole question and now I have -4 downvotes. What the hell is this? – Aerial Sep 22 '14 at 19:15
  • @Xarialon Well, (un)fortunately, the SE rules are mostly enforced by community involvement, not just moderators. The community is not always the most perfect enforcer of the rules. – ajp15243 Sep 22 '14 at 19:18

1 Answers1

2

To answer the question as it is - there is no way to make two users communicate with each other using AJAX (XMLHttpRequest) directly (e.g. without a server that would relay messages for them). XMLHttpRequest requires one of your users to have a HTTP server running and it's impossible for a browser to act as a HTTP server.

What you're looking for is a WebSocket - it will allow you to build your own client-server architecture independent on the underlying HTTP server.

fstanis
  • 5,234
  • 1
  • 23
  • 42
  • So do I really have to use a WebSocket, because I was thinking about a structure like this: myComputer->buttonClick->ajaxPost(info) to the server. Server->getInfo()->saveIntoDB->postAction to the other player. OtherPlayer->receiveInfo->updateContent; – Aerial Sep 22 '14 at 18:48
  • The problem with that flow is the *postAction to the other player* - there is no simple way for the HTTP server to "post" something to a client. One solution is polling the server (which is a horrible idea for a game), another is [Comet](http://en.wikipedia.org/wiki/Comet_%28programming%29) (not a library, but rather a method of using XMLHttpRequest), but there is no good reason to do it that way when you can make it much simpler and more responsive using a WebSocket. – fstanis Sep 22 '14 at 18:52
  • 1
    That's entirely possible, Xarialon. There will be as many different answers as there are server and database technologies. If you edit your question listing the type of server and database you have access to, you may get a good response. @fstanis 's response looks good for peer-to-peer. – Rick Hitchcock Sep 22 '14 at 18:55
  • @RickHitchcock I hope that it is better now. Do me a favor and upvote it because I did my best to recover that what needs to be recovered. – Aerial Sep 22 '14 at 19:14