2

How do I publish arbitrary data? This is what I want to accomplis, giving the following template:

<head>
  <title>Test</title>
</head>

<body>
  {{> hello}}
</body>

<template name="hello">
  <h1>{{greeting}} World!</h1>
</template>

I would like to dynamically update greeting on set intervals, using Meteor.setInterval. All examples in the documentation seems to be about Collections though.

Sven
  • 913
  • 8
  • 16

2 Answers2

3

You could use a Meteor Session variable as a reactive data source so the template automatically re-renders (http://docs.meteor.com/#session_set). Try this:-

if (Meteor.is_client) {

  // Use 'greeting' Session variable as a reactive data source
  Session.set('greeting', 0);

  Template.hello.greeting = function () {
    return "Welcome to test: " + Session.get('greeting');
  };

  Meteor.setInterval(function() {
    Session.set('greeting', Session.get('greeting') + 1);
  }, 1000);
}

if (Meteor.is_server) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

Updated: To show hooking into streams package. Example works but use at your own peril

if (Meteor.is_client) {
  // Use client from stream package
  sc = new Meteor._Stream('/sockjs');
  sc.on('message', function(payload) {
    var msg = JSON.parse(payload);

    // Set session variable so template reacts
    Session.set('greeting', JSON.stringify(msg.data));
  });

  // Use 'greeting' Session variable as a reactive data source
  Template.hello.greeting = function () {
    return Session.get('greeting');
  };
}

if (Meteor.is_server) {
  // Use server from stream package
  ss = new Meteor._StreamServer();

  // register handler for socket connection
  ss.register(function (socket) {
    var data = {socket: socket.id, connected: new Date()}
    var msg = {msg: 'data', data: data};

    // Send message to all sockets
    _.each(ss.all_sockets(), function(socket) {
      socket.send(JSON.stringify(msg));
    })
  });
}
Jabbslad
  • 546
  • 3
  • 10
  • Thanks, but I would like this to be synced between clients. Updated at the same time with the same message. – Sven Aug 30 '12 at 21:07
  • Ah ok I see what you mean. The `Meteor.Publish(..)` and `Meteor.Subscribe(..)` functions are solely for `Collections` which is why you couldn't find anything in the docs. You could either use a `Collection` to sync data between client and server although I agree that seems quite clunky if you only want to post an arbitrary message to clients. Another 'hack' you could try is hooking into the underlying `SockJS` library or the `streams` package. The `streams` approach is going to be simpler but also riskier as the methods havent been exposed to us and their implementation is liable to change. – Jabbslad Aug 31 '12 at 12:56
  • I've added a basic example of illustrating how to hack into the `streams` package included with `Meteor`. It shows the connection time of the latest socket opened with the server. If another client connects all clients are updated with the latest socket time. – Jabbslad Aug 31 '12 at 13:04
  • If you still want to push to the clients at intervals using `Meteor.setInterval` you could do so by just iterating over `all_sockets()` within a function. – Jabbslad Aug 31 '12 at 13:13
1

Hi checkout this new Streams Package. You can simply use it for your usecase

sc = new Meteor.Stream('hello');

if(Meteor.isServer) {
  Meteor.setInterval(function() {
    sc.emit('a_random_message', 'Random Message: ' + Random.id());
  }, 2000);

  Meteor.permissions.read(function() { return true });
}

if(Meteor.isClient) {
  sc.on('a_random_message', function(message) {
    Session.set('a_random_message', message);
  });

  Template.hello.greeting = function () {
    return Session.get('a_random_message');   
  };
}
Arunoda Susiripala
  • 2,516
  • 1
  • 25
  • 30
  • This is useful. Is there any way to integrate this into iron-router - i.e., have a waitOn() method wait on a Meteor Stream? – Pallavi Anderson Jan 09 '14 at 04:09
  • Not yet. Streams API is bit awkward. Looking for some new ways to implement with Reactive Data Sources. Will release soon(not so) with the new DDPPlus package I'm working on. – Arunoda Susiripala Jan 09 '14 at 07:50