2

I want to notify the user if there is a new mail -> so I created a new <div> in which I call a helper function and display the notification using notify js function. It is working correctly as is BUT if I try to run it say every 3 min. it doesn't respond... please provide some guidance.... thanks in advance.

MY CODE:

div in application.html.erb containing the partial for the div

    <div class="recentHashMailNotify" ><%= render partial: 'layouts/recentHashMailNotify' %></div>

The JS function to load the controller

    <script type="text/javascript">
        $(document).ready(function() {setInterval(function() {
        $('.recentHashMailNotify').load('/application_controller/recentHashMailNotify');
    }, 2000);
});
</script>

The action in the controller

  def recentHashMailNotify
  render :partial => "layouts/recentHashMailNotify"

end

The partial for the div

<% @recent = notifyHash %>
    <% if @recent.any? %>
        <% @recent.each do |elem| %>
            <script type="text/javascript">
              $(document).ready(function(){
                $.notify({
                    icon: 'pe-7s-science',
                    message: "<%= elem %>"
                },{
                    type: 'info',
                    delay: 0,
                    placement:{
                    from: 'bottom',
                    align: 'right'
                    }
                });
            });
            </script>
        <% end %>    
    <%end%>

The notifyHash function

def notifyHash

  @imap = Net::IMAP.new(.....)
  @imap.login(......)

  @imap.select 'folder'

  @recent = Array.new

  count =0
  @imap.uid_search(["RECENT"]).each do |uid|
        header = @imap.uid_fetch(uid, "ENVELOPE")[0].attr["ENVELOPE"]
        sub = header.subject 
        count += 1
        @recent.push(sub) if sub.include? "#"
  end

  @recent.push "You have got #{count} new mail" if count != 0

  @imap.logout
  @imap.disconnect

  return @recent
end
6
  • It is a helper function that I have defined which checks if there are new mails or not. If there are new messages it returns the subjuct of those messages as an array of string @trh Commented Apr 11, 2016 at 18:58
  • i have now added the notifyHash function @trh Commented Apr 11, 2016 at 19:04
  • When you say it's working, you mean it runs when the page first loads and not again? Commented Apr 11, 2016 at 19:25
  • What is the error message? Commented Apr 11, 2016 at 19:43
  • @trh exactly! but thats not what I want... I want to refresh it automatically Commented Apr 11, 2016 at 20:16

1 Answer 1

1

There might be a better way to do this. Create a js that has a setTimeout for your interval time, then call a function to do an ajax request and fetch your new messages. Return the new messages as json and process each one through notify, then at the end of the function, reset your timer. Note that in this example it's refreshing every 15 seconds, which is far too quickly and would be annoying :)

Your js might look something like

$(document).ready(function() {
  setTimeout(updateNotifications, 15000);
  function updateNotifications() {
    $.ajax({
      url:"/check-messages",
      dataType: "json",
      success:function(msgs) {
        $.each(msgs, function (key, data) {
          $.notify({
            icon: 'pe-7s-science',
            message: data
          },{
            type: 'info',
            delay: 0,
            placement:{
              from: 'bottom',
              align: 'right'
            }
          });
        });
      }
    });
    setTimeout(updateNotifications, 15000);
  };
});

You'll need to create a route in config/routes.rb

get "/check-messages", to: "users#get_messages"

Then the "helper" method that you have in application_controller.rb, move it to users_controller.rb (it's not Rails-esque to point a route to application_controller and can break things).

@imap = Net::IMAP.new(.....)
@imap.login(......)

@imap.select 'folder'

@recent = Array.new

count =0
@imap.uid_search(["RECENT"]).each do |uid|
      header = @imap.uid_fetch(uid, "ENVELOPE")[0].attr["ENVELOPE"]
      sub = header.subject 
      count += 1
      @recent.push(sub) if sub.include? "#"
end

@recent.push "You have got #{count} new mail" if count != 0

@imap.logout
@imap.disconnect

respond_to do |format|
  format.json { render json: @recent.to_json }
end

Also, @imap is an instance variable and unless you need it available in your views, you might move it to a local (remove the @ sign)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much @trh I finally got it to work correctly ! and your method seems quite elegant, will keep in mind next time on :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.