Building a Twitter feed using file events
Let's apply what we've learned. The goal is to create a server that a client can connect to and receive updates from Twitter. We will first create a process to query Twitter for any messages with the hashtag #nodejs
, and write any found messages to a tweets.txt
file in 140-byte chunks. We will then create a network server that broadcasts these messages to a single client. Those broadcasts will be triggered by write events on the tweets.txt
file. Whenever a write occurs, 140-byte chunks are asynchronously read from the last-known client read pointer. This will happen until we reach the end of the file, broadcasting as we go. Finally, we will create a simple client.html
page, which asks for, receives, and displays these messages.
While this example is certainly contrived, it demonstrates:
- Listening to the filesystem for changes, and responding to those events
- Using data stream events for reading and writing files
- Responding to network events
- Using timeouts...