Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Nithin KV
Nithin KV
809 Points

How to get real time data from mysql DB in browser with Node.js

I want to build an application that helps to show real time data from mysql database with php and nodejs.

14 Answers

Joshua Eagle
Joshua Eagle
9,778 Points

Hey Nithin,

I had a feeling that is what you may have tried hehe. :) Using websockets sounds like the way you might want to go, and it is low cpu usage. It's a bit tricky to implement, but Firebase does a lot of this work for you, and their API is really easy to understand. Let me know if you need an example, and I can draw one up for you. :)

So the way I would do this. I would use the backend (PHP in this case) to listen for changes in the mysql, then retrieve the data and temporarily store it in firebase as a session. You can then setup a frontend session in javascript and you can easily see the data update beautifully and efficiently. Now, if you want the end-user to change the data, just make sure to have PHP listen for those changes in Firebase as well so that it can store it in mysql. I might be over thinking it, but this is what I would try first. :)

Nithin KV
Nithin KV
809 Points

If you can please provide any firebase integration link. Thanks

Joshua Eagle
Joshua Eagle
9,778 Points

I'll try to answer this as best as possible as I think there are many solutions to this, and I am not entirely sure of your environment setup.

If you want Node to get Real-time data from MySQL and display that on the DOM, there are several things you will have to do (Frontend and Backend) in order to make this work, and it may not be very efficient if we took this approach. Might I suggest looking up the use of Web Sockets, or perhaps even Firebase to achieve your goal. Firebase, allows you to show data in real time even if someone else was to change the data from anywhere in the world, you would see it almost instantaneously. I hope this helps, I would need to know more in order to help you more. :)

Cheers.

Nithin KV
Nithin KV
809 Points

Thanks Joshua Eagle,

My application is based on Php Zendframework. I tried Java Script setInterval function with ajax. It's working perfectly. This is running in each seconds. The problem with this is, it's consuming high CPU usage.

Nithin KV
Nithin KV
809 Points

Hi Joshua,

Thanks. I didn't try Firebase as much. Basically my first requirement is I want show the user count in a page and it should change in realtime without refreshing.

I will try Firebase. If you could provide any link for this really helpful :).

Thanks very much :) !

Joshua Eagle
Joshua Eagle
9,778 Points

This should help you achieve this goal real easily: https://firebase.google.com/

I'm going to see if I can create a simple example of at least showing the data to the user for you with firebase, and a place to update the data, and if you have two browsers open you should be able to see it update in both places instantly. :)

Nithin KV
Nithin KV
809 Points

Thanks Joshua !! much appreciated :)

Joshua Eagle
Joshua Eagle
9,778 Points

There is no PHP here, but here is a great example. Try running it in your apache server, and open it in two browsers. It should achieve what you are looking for. :)

https://github.com/eaglejs/googleFirebaseExample

Nithin KV
Nithin KV
809 Points

Thank for the example, It's really helpful. I setup my account in firebase and got the config code :). Can I connect my mysql DB with Forebase DB.

Joshua Eagle
Joshua Eagle
9,778 Points

Well, I don't think you can "connect" them in a clean way. I'm not sure what all you have in your mysql Database, but you may not even need mysql in this instance. But if you wanted to. You might have to look up a way on how to do this with PHP. I'm no php expert. But there are two choices that I can see. Either figure out how to use PHP to connect with your Mysql and update firebase based on the data of mysql, or simply use firebase straight. Either way, You will probably need PHP to figure this out. It's up to you. I found this php client for firebase. Hopefully this can get you in the right direction.

https://github.com/ktamas77/firebase-php

Nithin KV
Nithin KV
809 Points

Actually I need to use MySql, its not just user count I may need to use other functionalities also. I want to find a way to connect my current MySql Db with firebase. Current Mysql has around 50 Tables can't change that :(.

Nithin KV
Nithin KV
809 Points

I found this https://zapier.com/zapbook/firebase/mysql/. what do you suggest on this?

Joshua Eagle
Joshua Eagle
9,778 Points

That might work, I have never used it. Give it a shot! :)

Nithin KV
Nithin KV
809 Points

Hi Joshua,

Could you provide any links or tutorial related to websocket integration. I tried firebase, but we can't connect mysql with Forebase DB. we need to duplicate Db's for this. So I think lets try websocket. I'm new to nodejs, but I love to learn this.

Thanks Joshua for your supports

Joshua Eagle
Joshua Eagle
9,778 Points

Give these a shot. I know Node.js example is a little more involved, an you can omit a lot of this code, but the concept is there.

http://code.tutsplus.com/tutorials/using-nodejs-and-websockets-to-build-a-chat-service--net-34482

Alternatively, I believe PHP has a plugin that might be able to help you. This might be more the route that you might want to take. Since it adheres to your stack.

http://socketo.me/

Nithin KV
Nithin KV
809 Points

Thanks Joshua Appreciated :)