hackernews-realtime-listener
v2.0.1
Published
Get realtime Hacker News updates
Downloads
7
Readme
Hacker News Realtime Listener
This JavaScript package allows you to listen to Hacker News stories and comments in real time. It uses the Hacker News API.
Usage
This package exports a default function called listen()
that accepts a required callback function as its argument.
Example:
const { default: listen } = require("hackernews-realtime-listener")
const listener = listen(item => {
// Do something with the item here.
// For the item schema, see https://github.com/HackerNews/API#items
console.log(item)
})
With advanced options:
const { default: listen } = require("hackernews-realtime-listener")
const listener = listen(item => {
// ...
})
Note: When listen()
or start()
is called, it immediately triggers the callback with the latest item.
To stop listening, just call listener.stop()
, and to start listening again, call listener.start()
(the listener is the return value of the listen() function).
Pretty simple, isn't it?
Lower level API
This package also provides a lower level object-based API.
Example:
const { RealtimeListener } = require("hackernews-realtime-listener")
const listener = new RealtimeListener(() => {
// The callback...
}).start() // You can also call start() later on the RealtimeListener object, just like listen().
Usage in the browser
To use this package in the browser, you can use the Skypack CDN:
<script type="module">
import listen from "https://cdn.skypack.dev/hackernews-realtime-listener"
// ...
</script>
Right now, we don't have a minified JS bundle. Maybe in the future.