NodeJS communicating with other NodeJS instances and managing minecraft server - javascript

I was wondering how would I communicate with another NodeJS instance? For instance I have one NodeJS instance that is a chat room, how would I get the chat and all people connected to the chat from another NodeJS instance.
Secondly I was also wondering is it possible to manage Minecraft servers using NodeJS so for example create a directory copy all the necessary files then start the server with x amount of ram and be able to receive output of the server and send console commands.

You have many options, Socket.io, Rest, Soap, TCP/IP or even low-level protocols. Really depends on what is supported by Chat node. If you are the owner of both nodes, then I would suggest to use Socket.io, it is more real time and supports push-based communication, otherwise you will have to periodically hit REST or SOAP API of remote node.
As far as NodeJS is concerned it can execute shell commands for example. From there it is upto Minecraft what it offers and adheres to.

Related

Internet of Things design for jersey and atmosphere

I have a fully working Jersey/Rest application server on an embedded device and am in the process of converting it to websockets with atmosphere-jersey to make it available through a firewall. I've just run into some design questions.
I have around 125 different rest call endpoints. I setup a websocket to a few of them and transferred data back and forth, similar to rest, but with live pushes. Since I built a socket with a subscriber for each endpoint, does this mean I'm actually maintaining a websocket on the browser side for each connecting endpoint? Or is the browser smart enough to hold a single socket open to the same domain and send requests back and forth to each endpoint? If I am maintaining a lot of websockets, then is there a particular strategy to do all communication with multiple endpoints, using a single websocket?
As well, my project is going to require an intermediary service to match up a login to a device of registered socket listeners. Is there a container that takes care of matching up logins to a websocket broker, that I could host with my own webservices (must be free)? Since all of my backend services look like rest, I don't want to have to subscribe each endpoint to the intermediary; so I'm wondering if I need to setup a single websocket broker to handle the traffic and push it out to the endpoints, or if the jersey-atmosphere service is smart enough to handle this?
Edit: added a design question:
In order to communicate between a web browser and a back end server using a single websocket interface; Is there a clean and easy way to generate a POJO for each receiving broker, or will I have to do a JSON conversion as the first step in each class that receives an object? If I build a javascript message with some sort of key to determine the broker, then I could map the key to a class and do a pojo generation for passing back the object to the handler: but it seems like this is a bit clunky and coupled.

Express web sockets and a central server

I'm creating an app where the server and the clients will run on the same local network. Is it possible to use web sockets, or rather more specifically, socket.io to have one central server and many clients that are running native apps
? The way I understand socket.io to work is that the clients read the web-pages that are served from the server but what happens when your clients become tablet devices running native apps instead of web pages in a browser?
The scenario I'm working with at the minute will have one central server containing a MEAN app and the clients (iPads) will make GET requests to the data available on the server. However, I'd also like there to be real-time functionality so if someone triggers a POST request on their iPad, the server acknowledges it and displays it in the server's client-side. The iPad apps will (ideally) be running native phonegap applications rather than accessing 192.168.1.1:9000 from their browser.
Is this technically possible to connect to the socket server from the native apps or would the devices have to send POST requests to a central server that's constantly listening for new 'messages'? I'm totally new to the whole real-time stuff so I'm just trying to wrap my head around it all.
Apologies if this isn't totally clear, it's a bit hard to describe with just text but I think you get the idea?
Correct me if I am wrong.
You have multiple iPads running native app. They send a POST request to your node JS server which is running in a computer in the same local network. Whenever the server receives a request from app, you want to display that a request has been received in your computer screen.
If my assumptions about the scenario is correct, then it is fairly easy to do. Here are the steps to do it.
Create a small webpage (front end). Load socket IO in the front end page like this -
<script type="text/javascript" src="YOUR_SERVER_IP/socket.io/socket.io.js"></script>
Then connect to server using var socket = io(). This should trigger connection event in your backend.
Handle all POST request from apps normally. Nothing special. Just add a small snippet in between. socket.emit('new_request', request_data). This sends new_request event to front end.
Handle the new_request in your front end using socket.on('new_request', function(request_data) { ... });. That's it. No need to add anything to your native app for realtime update.
The second step would be a little complicated as it is necessary to make socket variable available inside all POST requests. Since you chose node.js, I don't think you need any help with that.
Not totally clear on your project, but I'll try to give you some pointers.
An effective way to send data between native apps and a server is using a REST server. REST is based on HTTP requests and allows you to modify data on the server, which can connect to your database. The data returned is typically either JSON or XML formatted. See here for a brief intro: http://www.infoq.com/articles/rest-introduction
Android/iOS/etc have built in APIs for making HTTP requests. Your native app would send a request to the server, parse the response, and update your native UI accordingly. The same server can be used from a website using jQuery ajax HTTP requests.
Express.js is more suited to serving web pages and includes things like templating. Look into "restify" (see here: mcavage.me/node-restify/) if you just want to have a REST server that handles requests. Both run on top of node.js (nodejs.org).
As far as real-time communication, if you're developing for iOS look into APNS (Apple Push Notification Service). Apple maintains a persistent connection, and by going through their servers you can easily send messages to your app. The equivalent of this on Android is GCM (Google Cloud Messaging).
You can also do sockets directly if that's easier for you. Be careful with maintaining an open socket on a mobile device though, it can be a huge battery drain. Here's a library for connecting ObjC to Socket.IO using websockets, it may be useful for you: https://github.com/pkyeck/socket.IO-objc
Hope that helps!
To answer your question, it is definitely possible. Socket.io would serve as the central server that can essentially emit messages to all of the client. You can also make Socket.io listen for the messages from any of the clients and serve the emitted message to the rest of the clients.
Here's an example of how socket.io can be used. Simply clone, npm install, and run using 'node app.js'
All you have to do is to provide a valid server address when you connect your socket from the iPad clients:
var socket = io.connect( 'http://my.external.nodejs.server' );
Let us know if you need help with actual sending/receiving of socket events.
It is possible to connect to Websockets from your apps.
If you are using PhoneGap then you need a pluging that gives support to websockets in your app (the client) and then use websocket like normal way using Javascript see this.
If your app is native iOS look into this it could help you.
The primary use of the Sockets in your case is to be a bidirectional "pipe" between an app and server. There is no need of server sending the whole web-page to the native app. All what you need is to send some data from server to the client(app) in response to POST (or GET) request and then using this data on client side to update client's UI in real-time. If you are going to use moderate amount of devices (say tens of them), you may have connected all of them to the server permanently keeping individual socket connection open for every individual link server-to-app. Thus you may deliver data and update client's state in real time.
In fact web browsers also employ sockets to communicate to web servers. However as in general case there is no control on amount of concurrent clients in Internet, for the sake of limited networking resources conservation, servers do not keep sockets open for a long time, closing it just after the web-page was sent to client (or timeout has expired). That's how HTTP protocol works on the low level. The server waiting for the HTTP clients (browsers) by listening the 80 port, responding them by sending the whole web page content, then closing the connection and keep waiting for another requests on the same port.
In your case it's basically a good idea to use socket.io as it's a uniform implementation of sockets (ok WebSockets) on both client and server side. The good starting point is here

How To Configure webRTC And Express.js To Run In Same Domain

I'm looking to have a video conference website running on node.js + express.js instead of regular apache and I'm having trouble deciding on a solution.
I need the server to send the website to the user through express.js, plus, connect people in a queue so they can enter a video conference with strangers.
Should I build two different server applications, one for the express.js and one for the webRTC server, listening on different ports?
If so, how can I make the user interact with the different aplications? They will need to be able to login, be placed in the queue and be able to see the strangers once the connection is established.
WebRTC and Express.js is differ ExpressJS (Web frame work mapping your request)
but WebRTC is differ(Peer-to-Peer) communication with the help of signalling means exchange information about two browser so you can use Socket.IO or any other websocket framework
Every thing depend of signalling you want to connect particular person send signalling that particular person its not possible in Express.js so use any websocket framework like socket.io

Javascript client C++ server game

I have a question which path should I follow.
I want to develop real time online game via webbrowsers.
I would want to write game server using C++ with TCP sockets listening. The client side game will be written in javascript. The only problem I dont know how to communicate javascript with c++ server using TCP sockets. I have considered using Socket.IO but as far as I know this library does not have option to just connect to real TCP server, push bytes through and read incoming ones. Instead I would need to use some wrapper like Node.JS server which I want to avoid.
Anyone could guide me which path I should take?
You could make your game server itself an HTTP server. For the most part it could just serve up your static files, but when it received a WebSocket upgrade request, it could handle that however it desired.
You should take a look to websockify:
websockyfy is a WebSocket to TCP proxy/bridge. This allows a browser
to connect to any application/server/service. Implementations in
Python, C, Node.js and Ruby.

Peer to Peer, Javascript Games

I am writing a simple javascript game for a webpage. I am going to convert it to the desktop using tidesdk. I would like to allow players on different machines to play each other without the need to communicate through a server.
Is this possible in general? Is this Sockets?? Do you have any links of this being done with javascript code?
Is this possible with TideSdk? Do you know of any links to examples of this being done wiht TideSdk?
How do the players know what ip address/port their machine is on so they can give it to the other player?
I am sorry these are vague and open questions, but I don't really know where to start looking for this stuff, as I don't really know what the stuff I am looking for is called.
... Oh, and I don't want to use any third party stuff if I can help it. Maybe the jquery at a push.
This would be impossible with the APIs provided by web browsers (you would need to use something like Socket.IO and communicate through a server, as others have said). Fortunately, since you are using TideSDK, it is possible as long as you don't need a lot of network efficiency. You will need to provide a server, but it will not have to be powerful enough to host the actual games.
The General Client and Server Method
There are other ways to organize a network, but you can look those up if you think they'd be easier to implement.
Your server will host the actual game download and provide matchmaking capabilities. The clients that people download will contact this matchmaking server to find others who want to play.
The matchmaking server should select one of those clients to be a host for the others. Finally, the matchmaking server will tell the client selected as a host that it is the host and give it everyone's connection information (ports and IP addresses) while giving the other clients the connection information for the selected host. The host will connect to the other clients.
The host computer will be the only one that actually does any processing of gameplay, and the other clients just display whatever information the host sends them. The clients render the current state of the game from each player's perspective on their respective computers and capture user input, which is sent to the host for processing.
Implementation
TideSDK provides a Ti.Network.TCPSocket object which can make raw TCP client connections to TCP servers. Unfortunately, it does not also provide a way to make raw TCP servers. Instead, TideSDK provides a Ti.Network.HTTPServer object, which implements the HTTP protocol server over TCP, and a Ti.Network.HTTPClient object, which provides an HTTP client (it is actually just an abstraction over the normal AJAX request API). You can use the provided HTTP server on the host computer and directly connect to it on the clients using the provided HTTP clients. Data will be exchanged using the HTTP protocol. As far as I can tell, this is your only option here.
I did not find any example code out there (beyond what is in the TideSDK documentation) but you might find some if you are really interested.
Next Steps
If I wanted to go ahead with using TideSDK, I would do the following:
Tell the developers of TideSDK that you are interested in a TCP server socket. A raw TCP connection would be much faster than HTTP.
Test out the HTTP connection and find out if it is fast enough for my game.
Yes it's possible in general, and sockets are what you need. Although I don't think it's possible in practice, here's why.
Normally in a P2P game, there would be a server that knows who is online, and what their IP is. When new players connect to the server they will see a list of other users, they can select who they want to play.
Without having the server, there will be no way for users to see who is online, and to answer your 3rd question:
How do the players know what ip address/port their machine is on so they can give it to the other player? It doesn't matter if they can find their own IP, they have no way to find the IP of the opponent (without calling them on the phone :)).
So, if you want to build a game, then you'll need a server. I suggest Node.JS alongside Socket.IO

Categories