In time for our first JS Oxford meet - I put together a small node app which reads geocoded tweets from the twitter streaming api and pushes them to the browser to display on a map.
This is a stripped down version of a project that I worked on at White October this summer. This version is not at all for production use (your browser will grind to a halt if you leave it running for a while!), though I hope it’s a good/interesting example of linking up server and client js.
The code is all up at the jsoxford github account, I’ll go over a few bits of it:
app.js
This is the main node.js file it brings in some external packages:
- ntwitter - for accessing the twitter streaming api
- express - to serve some static files
- faye - for sending messages between the server and the browser
Once these have been brought in, you can connect to the streaming api using ntwitter. This gives you access to a stream
object, which you can add listeners for new tweets using the stream.on()
function (see eventEmitter docs for more details).
We then want to serve some static files for our client side pages/scripts, you can use express to do this (express can do a whole lot more - if you want to have a look, I’d recommend using the executable to generate a basic app).
We also want to send data to the browser using faye, this has a really nice pubsub api based on the bayeux protocol. Attaching this to the http server will listen for websocket/ajax long-polling requests and serve a client js wrapper at /faye.js.
Now, to link it all together - you can listen for events on the twitter stream, then publish them to a faye channel with the following code.
markers.html
Moving clientside (this code is in ./public and will be served to the browser), we first want to connect to the faye pubsub. To do this, we include the faye client library and connect to the endpoint that we mounted faye at on the server using Faye.Client
.
We’re using the google maps api to display the map and place the markers. The majority of the code for this is straight from the simple-markers example. (To get more of an introduction - have a look at the tutorial).
To get the tweet data from Faye, you use the client.subscribe
function to listen to a channel - in this case we broadcast them over ‘/tweet’ from node.
And that’s it! Have a look at the code on github (I’ve missed out a little bit of the surrounding bumf above) and have a play with it.
Also, if you are based around Oxford - come along to our next JSOxford meet on the 17th of January.