Search code examples
javascriptparse-platformserver-sent-eventssparkcore

SSE event stream with Parse CloudCode


I am trying to create a SSE event stream with Parse CloudCode so it can consume data sent from a SparkCore. I have it working fine if embedded in a webpage, I'm just unsure of how to do it Parse CloudCode. Here is JavaScript I am working with:

document.getElementById("uptime").innerHTML = "Waiting for data...";

var deviceID    = "XXXXXXXXXXXX";
var accessToken = "XXXXXXXXXXXX";
var eventSource = new EventSource("https://api.spark.io/v1/devices/" + deviceID + "/events/?access_token=" + accessToken);

eventSource.addEventListener('open', function(e) {
     console.log("Opened!"); },false);

eventSource.addEventListener('error', function(e) {
     console.log("Errored!"); },false);

eventSource.addEventListener('Uptime', function(e) {

      var parsedData = JSON.parse(e.data);
      var tempSpan   = document.getElementById("uptime");
      var tsSpan     = document.getElementById("tstamp");

      //Display data on webpage
      tempSpan.innerHTML = "Core:" + parsedData.coreid + " | Data: " + parsedData.data;
      tempSpan.style.fontSize = "28px";

      tsSpan.innerHTML = "At timestamp " + parsedData.published_at;
      tsSpan.style.fontSize = "9px";

}, false);

What I would ideally like to do is take in parsedData.data and create a new row in my Parse database.

Does Parse support SSE streams??


Solution

  • random, I would say no. The SSE protocol requires you to send the REST Get request and then listen to inbound TCP stream indefinitely to capture the stream of JSON objects being sent continuously. I would imagine the REST client implementation at Parse doesn't handle this kind of unknown/indefinite response length. I