So, Im here today wondering if its possible to call commands from the node.js console that can be specified in functions in the code...
For Example ( Just a stupid hello world example ), but say i could run the console, and i could type a string into the console, and it would run it.... Or just simply as defining a custom command and having it call to a function...
My goal here is to be able to build a interface for Turntable, so i could possibly have the bot kick a user by defining a function of kickuser username reason, just by calling it from the console....
Any help would be extremely amazing...
Thanks,
You can do that in many ways.
The most common is to create a URL that only can be accessed by localhost that run the command you want to kick the user, then you can use curl or create a node script that trigger that url passing the desired params.
Another way, more advanced, is to listen for socket connections from local, and expose the methods you want to execute to do jobs. Then you can create another script that when run, will connect the socket and send commands for your server.
Some projects that may help you:
Node Console.io
Node Inspector
Related
I've recently build an socket with Ratchet (http://socketo.me/) in Symfony ( PHP framework ) with JS/jQuery. The problem im facing is that the socket IP+PORT are just visible in the js file, so everyone can connect with it using the console or something.
I'm executing several things with the socket, that impacts everyone's view that's connected with it.
Now I'm just able to open a new connection through the console using:
var socket = new socket('http://www.IP:PORT');
Then I'm able to use the send command to execute things that only the server should have access to. Like this:
socket.send(JSON.stringify({info: 'information', action: 'runAction'}));
Is there anyone that can explain me how to keep these send calls privately so not everyone can just have access to these actions and call them?
You try to have these things stored in your php code and make an api say /getSocketDeatils
Now make an authenticated REST call to your api and make the socket when your app starts.
This is just one way there can be multiple other
I recently start using AWS, and read its docs.
For my needs, I must deploy EC2 instances from my node.js code, wait they are added to my ECS cluster (via using the right Arn Instance Profile), then start a task on the last EC2 instance started.
But, actually, I didn't find a way to define a task at the EC2 start, because I must override the docker command with a variable from my nodejs and do some other task. So, I must wait the EC2 being added to the ECS cluster before try to startTask() with my params and the Arn from the Container Instance Id (but I can't get it from a listContainerInstances() until).
Is there a way to achieve this easily?
(Actually I try to loop with a setTimeout until the listContainerInstances() return something new, but I don't think it's a good practice at all and my code look a little bit ugly).
Both the EC2 and ECS classes in the Nodejs SDK have waitFor which allow you to loop until a certain condition is met. It'll loop once every 6 seconds up to 100 times.
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#waitFor-property
How are you launching into the ASG?
Your best bet is to make a call to describeAutoScalingInstances and get the instanceIds. Set your instance to launch, find it's instanceId by getting the one not in the previous call. Then set a loop with a max attempts and a sleep timeout to wait until it's ready. You'll have to make a call with listContainerInstances for your cluster to know when it is ready.
When it's ready you'll want to run the task on it and override the command there. You can pin the task to a specific instance via the SDK and CLI, but not in the UI
What have you tried?
Have you tried using User Data to send commands to your EC2 at startup? For instance, I pass the following user data into my ECS container instances so they will update themselves and attach to a cluster:
#!/bin/bash
yum update -y
echo ECS_CLUSTER=<my cluster name> >> /etc/ecs/ecs.config
If you're not using autoscaling groups you can also tag your instance with a name in here, etc.
You should be able to run your own code here as well, and then your container instance will be ready by the time it is attached to your cluster.
You should use the node js ecs api describeClusters.
var params = {
clusters: [
'STRING_VALUE',
/* more items */
]
};
ecs.describeClusters(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
The response returns the count of registered instances in the attribute "registeredContainerInstancesCount"
Take a count of instances before you start the ec2 instance and wait till the count goes up by 1.
You can use a library like deasync to synchronize the wait.
I'm using nodejs, and one of the reasons why I switched from a php socket server to nodejs is because of the threading ability. (Essentially, I wanted my monsters in the gameserver to auto attack players).
Let's say in my sever.js file for node I put:
setInterval(function(){
console.log('Hello');
}, 1000);
And I login and authenticate my character on one browser, then look at the console I can see 'Hello' being outputted every second. That's fine, but then I load up a new browser, authenticate another user and then look at the console.. It's actually outputting twice as fast, which is not really the correct way to do this right?
Edit: I'm using https://github.com/websockets/ws and the setInterval function is just under the
socket.on('message', function(Message, flags) {
~~~gameserver authentication /blah mysql blah ~~
setInterval(function(){
console.log('Hello');
}, 1000);
})
Hope this helps, sorry for not being specific enough.
Your script is run for each user (since that is the server). You can listen and emit to a specific user of course. You need to generate an emit for each one, or write the emit in such a way it sends data only to the desired clients.
This may help you: socket.io and node.js to send message to particular client
Edit after comment:
No, the script will be run for each user so you start an interval for each. If you want to start only one you can:
1. Name your interval and if it is defined not start it again.
2. Start the interval on a separate script that you run from console or something like that and it is never accessed by clients.
What I want to do is simple in theory, but I cannot quite get it to work.
I wrote a simple node.js script that uses the request package to asynchronously fetch some data, parse it, and spit it out as html. I wanted to integrate this script in my client's php and apache based website which is on a shared host, and ran into some snags:
There is no mod_proxy, so I can't simply run my node script as a server and proxy through Apache
I don't want to run node on port 80 and proxy to apache from node. It's just way too much overkill for what I need to do, and would introduce too many headaches for me. My particular shared host is known to have trouble keeping node server instances up, and I can't justify potential downtime just for this script to run.
I tried the node-cgi package but it didn't work for me. I got errors about internal node methods not existing, I think this package is just out of date.
So what I have landed on is trying to simply call node from PHP. My whole index.php file is:
<?php
header("Content-Type: text/html");
exec("node beerlist.nd", $output);
echo implode('', $output);
When I execute php index.php on the command line, I get my expected output, however, when I try to access this from the browser, I get nothing ie Content-Length: 0. Why?
I thought maybe it had to do with the async nature of my node script but it seems like the process stays alive until it finishes all the async calls. So shouldn't my php snippet send the output to the browser without any trouble? What am I missing here?
Edit: This gist of my node script is
var req = require('request')
req("http://mywebsite.com", function(err, resp, body) {
var output = // pull some interesting pieces out of the whole body
console.log(output);
});
The generation of my output variable is not central to the issue here. The relevant parts are that I use request to make an asynchronous call and use console.log to output my results... maybe this is a problem?
I suppose Apache user doesn't know what node command is. If I'm right try to write in php file:
<full path to node> beerlist.nd
instead of
node beerlist.nd
To get full path to node run in terminal which node
I'm trying to determine how to setup a web socket for the first time ever so a working minimal example with static variables (IP address for example instead of getservbyname) will help me understand what is flowing where.
I want to do this the right way so no frameworks or addons for both the client and the server. I want to use PHP's native web sockets as described here though without over-complicating things with in-depth classes...
http://www.php.net/manual/en/intro.sockets.php
I've already put together some basic JavaScript...
window.onload = function(e)
{
if ('WebSocket' in window)
{
var socket = new WebSocket('ws://'+path.split('http://')[1]+'mail/');
socket.onopen = function () {alert('Web Socket: connected.');}
socket.onmessage = function (event) {alert('Web Socket: '+event.data);}
}
}
It's the PHP part that I'm not really sure about. Presuming we have a blank PHP file...
If necessary how do I determine if my server's PHP install has this socket functionality already available?
Is the request essentially handled as a GET or POST request in
example?
Do I need to worry about the port numbers? e.g. if
($_SERVER['SERVER_PORT']=='8080')
How do I return a basic message on the initial connection?
How do I return a basic message say, five seconds later?
It's not that simple to create a simple example, I'm afraid.
First of all you need to check in php configuration if the server is configured for sockets with the setting enable-sockets
Then you need to implement (or find) a websocket server that at least follows the Hybi10 specification (https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-10) of websockets. If you find the "magic number" 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 in the code for the header, you can be sure it does follow at least Hybi06 ...
Finally, you need to have access to an admin console on the server in order to execute the PHP websocket server using php -q server.php
EDIT: This is the one I've been using a year ago ... it might still work as expected with current browsers supporting Websockets: http://code.google.com/p/phpwebsocket/source/browse/trunk/+phpwebsocket/?r=5