Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including forecast.io.live with all npm packages installed. Try it out:

var forecastIoLive = require("forecast.io.live")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

forecast.io.live v0.1.4

Wrapper for forecast.io API using Socket.IO

Build Status npm version

forecast.io.live

Wrapper for forecast.io API using Socket.IO

You can find here the repository for the example app using forecast.io.live

How to use

npm install forecast.io.live

Server side

var io = require('forecast.io.live')(Server, API_KEY[, Options]);

Server : http.Server instance.

API_KEY : A valid api key.

Options : An object, with the following properties :

{
  timeInterval(ms),
  timeout(ms)
}

timeInterval : The time between each API call.

timeout : timeout for each request.

###Client side

Make sure to include socket.io in your code :

<script src="/socket.io.js"></script>

then establish the connection with your websocket server :

var socket = io.connect(url,
 { 
   transports:['websocket'],
   'force new connection': true
 }
 });

That's it ! , we can now emit a subscribe event and listen for a forecast event.
####socket.emit("subscribe", query) Once we subscribe with a query object, we receive the real-time data pushed by the server, we will call it a stream, each stream is identified by its query object, the query object have the following properties :

{
   'lat': LATITUDE,
   'long': LONGTITUDE
}

These two properties are required, but you can pass others as well such as lang, units. All the available options are explained in the API docs.

To receive the real-time data, we listen to the forecast event. ####socket.on("forecast", function(data) { ... })

The data argument contains the response from the server.

Note : if we emit multiple subscribe events, we should listen to a single forecast event, the data argument will contains the real-time data for all our subscribed locations.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free