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

General Discussion

Keith Monaghan
Keith Monaghan
9,494 Points

Server PUSH instead of AJAX...what are the options?

I'm creating a chat app using AJAX. Instead of having the server hit with requests every 2.5 seconds and to provide a quicker service, what are the options for pushing the DB updates from the server to the client?

I'm just looking for some technology suggestions.

Thanks!

5 Answers

Keith Monaghan
Keith Monaghan
9,494 Points

Looks like WebSocket does this kind of thing. Is it a good solution for a web app that needs to have a fair amount of dynamic content?

It really depends on your requirements.

Almost all of the latest browsers have support for Web Sockets, but you may run into compatibility issues with some users on older browsers.

Check this list against your estimated browser targets.

Also, check out html5please for a list of possible fallbacks and polyfills.

Keith Monaghan
Keith Monaghan
9,494 Points

Thanks John,

It looks like WebSockets with an AJAX fallback are the way to go.

Jim Hoskins
STAFF
Jim Hoskins
Treehouse Guest Teacher

WebSockets are the place to start. The real question is what kind of backend are you planning on using? Rails? PHP? Node.js? This is because the normal model of web applications is get a connection, send a response ASAP and close it. With push, however, the connection has to stick around for as long as you want to be able to push, and it's just not what rails and php were built for (though there certainly are hacks and solutions)

Node.js is your best bet for realtime web apps, because of the evented architecture, you can hold connections open and idle, while still servicing others (a lot of them in fact). http://socket.io is a great tool for websockets and a lot of fallbacks that allow you to do server push simply.

If your app does need to be on Rails, PHP, etc.. you can take a hybrid approach where you have your normal application, and a second app/service that is used for the push. Clients use your normal app, but the sockets connect to the socket app. When your main web app generates an event to push, it notifies the socket app, which relays the message. http://pusher.com/ is a hosted service you can use off the shelf, but you can also build your own solution.

Keith Monaghan
Keith Monaghan
9,494 Points

Thanks, Jim!

I really appreciate the feedback.

I'm starting from nothing with this project and I want to do it in the cleanest and simplest way. I've been using PHP but after the feedback here I'll be switching to node.js.

I'm just digging into JavaScript so I have a lot to learn.

Thanks everyone!