1
votes

So im trying to build ontop of the angular-meteor WhatsApp clone tutorial using Ionic 2 CLI

This tutorial basically deletes the client folder in the meteor project and uses the meteor-client-side package inside an ionic project to connect to the meteor server.

Which works perfectly fine, but now i'd like to subscribe to a meteor publication with an reactive parameter.

After searching the Meteor API Documentation I found the Session object:

Session provides a global object on the client that you can use to store an arbitrary set of key-value pairs. Use it to store things like the currently selected item in a list.

What’s special about Session is that it’s reactive. If you call Session.get("currentList") from inside a template, the template will automatically be rerendered whenever  Session.set("currentList", x) is called.

In the Meteor Subscribe Documentation you can find the following example:

Tracker.autorun(function () {

Meteor.subscribe("chat", {room: Session.get("current-room")});

Meteor.subscribe("privateMessages");

});

This subscribes you to the chat messages in the current room and to your private messages. When you change rooms by calling Session.set("current-room", "new-room"), Meteor will subscribe to the new room’s chat messages, unsubscribe from the original room’s chat messages, and continue to stay subscribed to your private messages.

Which is exactly what I want to do too. But as the Session documentation states, session is a package I have to add to the meteor project:

To add Session to your application, run this command in your terminal:

meteor add session

Now my question, is there any way to add session to the meteor-client-side packages?

If I just try to call Session.set() it fails on runtime with Session is not defined

My guess is that I would need some npm package that extracts the Session functionality (basically a sessions-client-side npm package) like accounts-base-client-side

Is there an other way to do this? How would I build my own sessions-client-side?

I tried to run meteor add session in my meteor project but was not able to find the code for Session anywhere in .meteor folder and npm_modules.

I also looked into the meteor GitHub but the Session.js file they have contains only documentation

Any input how to do something like this would be nice

Update:

I've looked into the accounts-base-client-side package and found out that they are autogenerated using a script, so im currently trying to adapt this script to work with Session instead of accounts-base. You can find my attempt at: https://github.com/AwsmOli/session-client-side

Still work in progress, but i should get it to work soon

Update 2:

See my answer, my session-client-side is working now :)

3
An alternative may be to use rxjs observables - you already have this library in your project from following that tutorial. - JeremyK

3 Answers

1
votes

The "Session" variable should just appear and be accessible. If you need to verify that, start a new project add the package and write some code to access it. It is likely that something has (unwittingly) nuked the Session variable - I have seen this before with another package.

Another way of doing this is with "getReactively". Below is a helper that uses it in a query. Make sure you declare it before the helper (otherwise it won't work). This one uses the result of another helper, but it can be any variable, and you just assign the variable for the reactivity to kick in and run the helper.

this.helpers({
  currentUser: () => { return Meteor.user() },
  elder: () => {
    let e = Elders.findOne({_id: this.getReactively('this.currentUser._id')});
    if (e) {
      utils.services.setupElder(e);
    }
    return e;
  }
});
1
votes

As per the meteor docs, you have to import it:

import { Session } from 'meteor/session'

This will enable it on the client.

In earlier meteor versions this was not required, as it was both a default package, and automatically imported into the global namespace.

1
votes

I ended up creating the session-client-side package myself, and its working nicely.

If you need it too, its available on GitHub: https://github.com/AwsmOli/session-client-side

and NPM:

npm install session-client-side

credits to idanwe who created the client side packages and made it realy easy to adapt his work to work with any meteor package :)

To use it with Ionic 2 Apps:

import it in your entry points (src/app/main.prod.ts & src/app/main.dev.ts)

import 'session-client-side';

and now the global Session variable is accessable form anywhere in your app:

 Session.set("aCoolNameFormyAwsmChangingObject", myAwsmChangingObject); 

Thanks for the help!