pmarks906d
Forum Replies Created
-
Forum: Everything else WordPress
In reply to: webpage communication using websocketsi almost forgot. For the above to work, you must start the Redis server using ssh, before running the em.js file:
./redis-server
Forum: Everything else WordPress
In reply to: webpage communication using websocketsHere are the beginnings of a solution: I learned how to start a background java script, and how to make a socket connection to it from a php template file. Then the script and the template can send messages back and forth over the socket. First I had to purchase a dedicated IP address from bluehost. Then I had to install node.js, socket.io, socket.io-server, socket.io-redis, and redis. Then I had bluehost open up ports 9100 and 6379. Then i had to make sure my php install is version 5.3 or greater. Then i configured ssh.
Here is the code for the background script. the file it is in is “em.js”. You can put the code in the .js file of your choosing.
// node server file emsave.js
var io= require(‘socket.io’)(9100);io.adapter(require(‘socket.io-redis’)
({ host: ‘your dedicated IP address here’})); // supply your dedicted ip address hereconsole.log(‘Server is listening on 9100’);
io.on(‘connection’,function(socket){
console.log(‘connection just made’);
socket.on(‘disconnect’, function () {
console.log(‘connection just closed’);
});
socket.on(‘message’, function (msg) {
console.log(msg);
socket.send(msg);
});
});to run the above, log in to ssh and type in the following command: node em.js
Here is the php template:
<?php
/*
Template Name: EmitterPHP
*/
/*
* @package WordPress
* @subpackage Twenty_Fourteen
* @since Twenty Fourteen 1.0
*/
?>
<?php
namespace myapp;
use SocketIO\emitter;
class OrganizationUpdates
{
public $host;
public $port = 6379;
private $_emitter;
/**
* Return redis queue
* @return emitter
*/
private function _emitter()
{
if (!$this->_emitter) {
$redis = new \Redis();
$redis->connect($this->host, $this->port);
$this->_emitter = new emitter($redis);
}
return $this->_emitter;
}
public function publishNewEvent($eventType, $data)
{
$this->_emitter()->emit(“news”, [
‘event_type’ => $eventType,
‘data’ => $data
]);
}
}
echo phpversion();
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<input type=”button” value=’Send message’ onclick=’sendMessage()’>
<input type=”text” id=”response” value=”this will change” >
<script src=”_https:_//_cdn.socket.io/socket.io-1.0.6.js”></script> // clean up this line by taking out the underscores
<script>
var socket = io(‘your website url here:9100’, { // put in your website url here
transports: [‘websocket’]
});
;
socket.on(‘message’, function (msg) {
document.getElementById(“response”).value=msg;
});
function sendMessage() {
socket.send(‘hi’);
};
</script>
</body>
</html>Opening a page using the php file template will make a connection (see console output of the em.js file). Then, hitting the ‘Send message’ button will send a message to the em.js process. The em.js process will log the message to the console and then send it back. The php page will display the returning message in the text box.
Forum: Everything else WordPress
In reply to: webpage communication using websocketsThe following is the simple server-side js test code which should run without errors if the redis sever were up and running.
// node server file emsave.js
var io= require(‘socket.io’)(9100);
io.adapter(require(‘socket.io-redis’)
({ host: ‘https://www.mysimplesurvey.com’}));
io.on(‘connection’,function(){
console.log(‘this should work’);
});Right now trying to run this code on my server’s ssh returns the following error:
[email redacted] [~/public_html/EmitterPHP]# node emsave.js
events.js:85
throw er; // Unhandled ‘error’ event
^
Error: Redis connection to https://www.mysimplesurvey.com:6379 failed – getaddrinfo ENOTFOUND https://www.mysimplesurvey.com
at RedisClient.flush_and_error (/home2/mysimpp7/public_html/EmitterPHP/node_modules/socket.io-redis/node_modules/redis/index.js:142:13)
at RedisClient.on_error (/home2/mysimpp7/public_html/EmitterPHP/node_modules/socket.io-redis/node_modules/redis/index.js:180:10)
at Socket. (/home2/mysimpp7/public_html/EmitterPHP/node_modules/socket.io-redis/node_modules/redis/index.js:95:14)
at Socket.emit (events.js:107:17)
at net.js:915:16
at process._tickCallback (node.js:343:11)
[email redacted] [~/public_html/EmitterPHP]#Forum: Everything else WordPress
In reply to: webpage communication using websocketsI just watched an online video by Guillermo Rauch called “Realtime Communication with Socket.IO and WordPress”. This has convinced me to use node.js, socket.io and socket.io-redis to accomplish my aims. So i installed node.js. socket.io, and socket.io redis. I bought a dedicated ip, and got tech support to open a port of my choosing for socket.io, and also port 6379 for redis. I have been able to communicate between two js scripts running on ssh. However i have yet to accomplish bidirectional communication between php (on a wordpress page) and a node.js socket.io server, using redis. From what i have seen on-line, I need to get a socket.io-redis sever up and running as one of the main steps. The demo by Guillermo is straight forward. But i have not been able to figure out how to get that redis server going. Does anyone know how to get a socket.io-redis server started on a bluehost site? Thanks, phil marks sr.
Forum: Fixing WordPress
In reply to: Accessing MySql database from page functionHi, I think I have gotten on track.I found a solution on line. If I put a form on my theme page, and have the submit button post from the local machine to the server to another php file, the following MySql code works inside that file. I’ve still got to work on security, however the basics seems to be there:
<html>
<body>
Welcome <?php echo $_GET[“name”]; ?>
Your email address is: <?php echo $_GET[“email”]; ?>
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
$sql = “INSERT INTO myCustomers(firstname, lastname)
VALUES (‘Fred’, ‘Marks’)”;
if ($conn->query($sql) === TRUE) {
echo “New record created successfully”;
} else {
echo “Error: ” . $sql . “
” . $conn->error;
}
$conn->close();
?>
</body>
</html>Forum: Fixing WordPress
In reply to: Accessing MySql database from page functionHi, Thankyou for your post. I’ve taken your advice and installed phpmyadmin as described. In the process I also put my code in a php theme file, which interprets PHP code correctly; then i chose that theme for my page. Again, however, everything still works great except when I try to access my MySql database. I have the following code in that theme file. The problem is the function never gets beyond the $wpdb part.
<script>
function registerFunction()
{
elEmailOut = document.getElementById(“emailReadId”);// this text box displays the right stuff if $wpdb commented out
elEmailOut.value = “got to here1”;// execution gets here if i comment out the $wpdb stuff
<?php
global $wpdb;
$wpdb->insert(‘myCustomers’,
array( ‘FirstName’ => ‘Fred’, ‘SecondName’ => ‘Marks’));
?>// execution never reaches here.
elEmailOut.value = “got to here2”;
/**
// more code here but commented out
*/
}
</script>