Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to get all messages after a datetime? #4

Open
ffabreti opened this issue Apr 9, 2016 · 4 comments
Open

Is it possible to get all messages after a datetime? #4

ffabreti opened this issue Apr 9, 2016 · 4 comments

Comments

@ffabreti
Copy link

ffabreti commented Apr 9, 2016

Dennis,
I am trying to make a chat with your gem where parent/children are chatroom/messages.
If I setup Message.all( ), everytime a message is created Message.all() is called and fetches all messages.
That is fine when you have a dozen messages, but if you have thousand of messages...
So, I thought of passing an object to .all(), like you can do on .create(), so that I can filter messages after some date on Rails controller. Something like:
Message.all({last: 'Sat Apr 09 2016 00:32:13 GMT-0300 (BRT)'}, function() {...} )

Would that be possible with current Entangled?
How and Where would be better to hack such a feature?

@ffabreti
Copy link
Author

ffabreti commented Apr 9, 2016

Well... this works:

entangled.angular.js:

Entangled.prototype.all = function(params, callback) {
      var aDash = (this.webSocketUrl.match(/\/$/)) ? '' : '/';
      var socket = new WebSocket(this.webSocketUrl + aDash + object2queryString(params));

Rails controller:

def index              # /chat_room/index/?last=2016-04-09T17:47:49.574Z
        broadcast do
            last = chat_room_last
            if last
                @chat_rooms = ChatRoom.where("last_time > ?", last).to_a
            else
                @chat_rooms = ChatRoom.all
            end
        end
    end

But the problem is parameters are statically setup and can never change (because webSockets URL is setup only once)

Maybe I should code a no standard action, not sure how much of the 'magic' will loose on that.

@dchacke
Copy link
Owner

dchacke commented Apr 18, 2016

What you need is what I would summarize as "scoping", such as where clauses etc. Scoping is currently not supported, but is on my todo list.

I'll leave this issue open until this is addressed.

@dchacke
Copy link
Owner

dchacke commented Apr 18, 2016

If it helps, and your performance concern is about the client (such as client side rendering), and not about the server, you can still filter your messages on the client after fetching them:

Message.all(function (err, messages) {
  if (!err) {
    $scope.messages = messages.filter(function (message) {
      return message.createdAt > 2 weeks ago // pseudo code
    });
  }
});

This would at least speed up the rendering of your messages. In this example, the client would ignore messages older than 2 weeks.

Just to be clear, if you have thousands of messages in your database, those will only be loaded the first time you invoke Message.all. After that, only new messages are passed, i.e. one at a time.

@ffabreti
Copy link
Author

ffabreti commented Apr 20, 2016

Thanks.
I am concerned about rendering, but also about network transfer time (slow connections like 3G).
Yeah, where clauses would be great for that. Maybe better than keeping track of a backlog and asking for message ids 'newer than'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants