javascript - Simple chat program in Meteor. How do I hook up a callback to fire when new messages come in? -


i'm working on simple chat app learn meteor , i'm little stuck here. have nicely working chatroom, i'm having trouble hooking function fire when chat comes in person. i'd automatically scroll bottom of window , update favicon notification when happens. have template chatwindow looks this:

<template name="chatwindow">     <div class="textwindow">         {{#each messages}}             <span class="username {{#if me}}me{{/if}}">{{username}}</span><span class="message">{{message}}</span>             <br />         {{/each}}     </div>     <br />     <form>         {{#if currentuser}}             <input type="text" name="chattextbox" id="chattextbox" />         {{/if}}     </form> </template> 

the {{#each messages}} block works great grabbing updated collection of chat messages automatically, how fire event when it's updated? i've tried template.chatwindow.onrender() , template.chatwindow.rendered both fire first time.

observe way go here. assuming messages stored in collection messages, can like:

messages.find().observe({   added: function (document) {     // set session vars or jquery or update other collections   } }); 

observe works on both client , server, , can register callbacks document changed events , document removed events.


Comments