Web sockets to grab live data from my server - php

I'm wanting to push real time data to my website via web sockets. The data is basically just text saying "Artist - Title", it is generated via my Windows server that is streaming live constantly.
Can anybody point me in the right direction (i.e a manual/knowledge site) where I can read up on what will be required? Right now I have a PHP script that is pulling data from the MySQL database on my server, that then drops it onto my site as described above (Artist - Title) - obviously this doesn't accomplish what I want as it requires people to refresh the site every time they want to see what's currently being streamed, what I want is for it to just appear the second it starts playing on my server.
I've tried researching into web sockets with no prevail to what I want to accomplish. If anybody has the knowledge as to what I'm missing to make this work I would be most appreciative of your answer.

Consider running a Node.js server with socket.io. See socket.io chat app tutorial and it should become clear.
Basically you need JavaScript on your page to open the socket and respond to messages.

Related

Simple data exchange between servers

I am completely new to web development, so please forgive me in case this question is superfluous.
Here is what I am trying to do:
I have multiple Arduinos (equipped w/ EthernetShields) that are collecting and displaying data (using sensors and LCD screens). All Arduinos communicate with a webserver hosting a MySQL database and a webpage visualizing the data. The Arduinos are themselves capable of hosting minimalistic webservers that can run some simple html/php/etc...
My question is:
What is the most straightforward way to implement the communication between the Arduinos and the main server, so that I can send data back and forth between them, without any user interaction?
The data consists of relatively few integer values and short strings of text. Security is not an issue.
Edit for clarity:
How to continuously send data back and forth between two web-servers?
Assuming that I use PHP, what is a simple way to do this? All tutorials for the GET and POST methods included the use of 'form action', which to my understanding requires user interaction.
If I understand you right, you have a central web server that needs to communicate with a whole bunch of arduinos. You need to send information in both directions between the arduinos and the central server. And, you want to use web (http / tcp / ip ) protocol to do this.
You face a choice:
Do you want to have the central server initiate the communication? Or do you want to have each arduino initiate the communication?
I think the second choice is pretty good. It means that you can add new arduinos to your system without somehow reconfiguring the central server. But, I don't know much about your application so there might be some reason this is a bad idea.
So, what you do is implement a simple web CLIENT (not a browser) on each arduino. Then, on a regular schedule you have the each arduino do a web request to the main server. Depending on the amount of data you need to send from the arduino to the server, you can use a GET or a POST. If the data is small, you can use a GET request. For example if it's temp and humidity you can send the request like this once a second.
http://server/upload.php?temp=65&humidity=78
In response to this, the central server can handle this data correctly. You'll be able to tell which arduino it's from by the client internet address REMOTE_ADDR.
Then, the server sends a response that contains whatever data the server needs to send to the arduino.
See how this goes? each second each arduino hits the web server saying "here's what I have for you. What have you got for me?"

Notifications in Node.js

I'm trying to achieve a notification system using NodeJS. Everything is going right: connecting to server, connecting to database, querying the database. Since I have just couple of hours working on NodeJS, I don't have the image clear how the notifications concretely happen. Googeling the issue didn't come actually with great things.
When someone posts a comment, the comment's inserted to the database, that should fire an event to notify his friends (in Friend table) that he posted a comment? Any type of help will be appreciated.
I'm using PHP and MySQL with "mysql" Node module.
Googleing "node.js notifications" I got this http://www.gianlucaguarini.com/blog/?p=272 or this http://gonzalo123.wordpress.com/2011/03/14/real-time-notifications-with-php/. It uses socket.io as Amberlamps suggests.
In the end you will want to have all of your users (their webbrowsers) run some javascript that opens a websocket (or otherwise, check out Socket.IO) to your node.js server. That connection stays open as long as they stay on the page. Once you want to send them a notification the node.js server does that through this connection. Your PHP script can trigger the node.js server through various ways, but a http request to localhost is probably the easiest and most realtime.

How can I simulate a peer-to-peer communications channel using PHP, MySQL DB and JavaScript?

The challenge I'm facing is simulating a communication channel between two users of a website (e.g. gaming site), by using solely the technologies mentioned in the title.
Recently I've developed an online chess website where, the idea behind which was to give users the chance to play "live" matches too, not just lengthy games where you would make a move, then come back in 1-15 days to see if your opponent has responded. And the way this game engine works implies sending asynchronous requests to the server, both to update the info related to the game (in case you make a move), but also to verify if anything has changed (if you are waiting for the opponent to move).
To better explain this... the player whose turn it is (the browser of course) sends an async. request to update the game info, exactly when he makes his move. Meanwhile, the opponent sends PERIODIC requests, "asking" if anything has changed. If anything does change, the roles switch after the board updates.
Timers are behind the functionality of the engine, so my question to you is this: How would you go about simulating a com channel between two players, while trying not to put too much stress on the server, but also having games update as fast as possible, in order to maintain the "live" feeling to it. This is most important in 1 minute games (one of the available categories). Because in that case, requests NEED to take place VERY often (at least 1 second). But server responses could delay, there would be much stress on the server when having hundreds of games available at the same time etc... so you see my problem.
I look forward to hearing from you guys and picking your brain if you have any good ideas :)
Cheers!
Andrei
PS: In case you want to try it, the name is e-chess960.com
you would want to builds a socket server. node.js would be a good javascript based library to use for a socket server. each client would create a socket connection to the socket server, then when a client sends a message to the socket server, the socket server could immediately send the message back out to its subscribers without having to store it anywhere.
socket servers require socket access, which usually requires a vps instead of a shared server.
to make the solution work, you would require that the clients have the ability to create socket connections to your server. some browsers already can as part of html5, but not all yet. you could also use Flash as a way to create a socket connection.
What you actually want is not pulling the server every second, but instead keep a connection open and use the observer-pattern to distribute updates to the specific clients (in other words: push instead of pull). That way, your server resources will not get swamped by a new connection for every second and every client. A library to accomplish that is the Comet Library. Check out this nice tutorial for a sample application.

Flash browser game - HTTP + PHP vs Socket + Something else

I am developing a non-real time browser RPG game (think Kingdom of Loathing) which would be played from within a Flash app. At first I just wanted to make the communication with server using simply URLLoader to tell PHP what I am doing, and using $_SESSION to store data needed in-between request.
I wonder if it wouldn't be better to base it on a socket connection, an app residing on a server written in Java or Python. The problem is I have never ever written such an app so I have no idea how much I'd have to "shift" my thoughts from simple responding do request (like PHP) to continuously working application. I won't hide I am also concerned about the memory and CPU usage of such Server app, when for example there would be hundreds of users connected. I've done some research.
I have tried to do some research, but thanks to my nil knowledge on the sockets subject I haven't found anything helpful. So, considering the fact I don't need real time data exchange, will it be wise to develop the server side part as socket server, not in plain ol' PHP?
Since your game isn't something that's working in realtime you probably don't need to go down the socket route, though it's certainly a viable option. The nice thing about sockets is that updates would be instant without requiring page refresh (or server poll), so you're right to at least consider it.
If you do want to do a more real-time server setup, you might consider using something like Electroserver - this abstracts out much of the setup for you so you don't have to write your own server from scratch, plus it's free up to a certain number of concurrent users if I recall correctly.
Finally, a third option you have is a modified POST approach using AMF. Look into AMFPHP, it lets you call methods on a PHP back-end directly from your flash application. A little bit faster and easier than simply using POST stuff, but not quite as seamless as a socket connection or a specifically built gaming server.
Lots of options out there, it sounds like you are aware of this and kudos for trying to come up with the best approach rather than just rolling with what you know! I hope this helps, let me know if you have any questions.
Here's a link to Electroserver - http://www.electro-server.com/

How to open a browser in PHP?

Im here to ask a probably dumb question again. This is related to my previous questions.
I current found a way for flash/flex to send a video file(in byteArray) to php. Where php receives and writes it in the local sytem then sends it to the youtube server automatically trhu a script.
This is all backend though I have no way to check if the video file was uploaded succesfully in youtube than to check it manually in youtube.
So im thinking of a new php script where it will receive and write the video file from flash. Then opening a browser window where it will automatically upload the video and at the same time track it thru the browser of its status using debug prints perhaps.
Im barely still learning php. I tried to do some research on this but found no success. Hope you can help me guys. Thanks.
PHP code runs on your web server. It's code running on another computer. It can't open anything on YOUR computer. If it could, I would also be able to open windows on YOUR computer from the one I'm typing on, and you'd have some serious problems. By tomorrow everyone in the world would have their computers either taken over or destroyed by other random people on the internet.
PHP is server side, you can't directly use any PHP code to control browser activity on client side.
You need to resort to client side technology such as Flash and JavaScript.
echo "XXX window.open('URL');XXX'
XXX = script tag from Java, I can't post html tags

Categories