Using Custom Collections
Empirica allows you to store data in different places for each game (e.g., game, round, stage, player, etc.). But you might want to store and use data in a special structure or have certain data accessible across all games (e.g., global configs, data from all participants). To do so, you need to create and use custom collections in your database. Thankfully, Empirica uses Meteor, which makes this process easy (see their collections API docs).
Note: you can use custom collections during development without connecting your experiment to MongoDB. However, it is easier to see what is happening if you are connected to a cloud database that you can monitor.

Using custom collections just on the server side

You need to create a file to connect to your custom collection. I usually put these in an api folder. Create a .js file for the collection of interest. For example, playerScores.js.
The contents should look something like this:
1
import { Mongo } from 'meteor/mongo';
2
​
3
export const PlayersScores = new Mongo.Collection('players-scores');
Copied!
The string you pass to the new Mongo.Collection is the name of the collection.
Then you can access this collection in your other server side file with an import statement, for example:
1
import { PlayersScores } from "./api/playerScores"
Copied!
Then you can use this collection by updating it, or setting it to elements that are easy to access on the client side (e.g., game, round, stage).
To get elements from the collection (example):
1
PlayersScores.find({}).fetch()
Copied!
To insert a new element to the collection (example):
1
PlayersScores.insert({ playerId: player._id, score: player.get("score") })
Copied!

Using custom collections on the client side

If you want to use the custom collection directly on the client side, you should set the files in a shared folder in the root of your experiment files so that it can be access both on the client and server side. Set the file for the collection as explained above.
In server/main.js , import the collection and publish it. For example with a 'admin-global-configs' custom collection.
1
import { Configs } from "../shared/api/collectionAdminGlobalConfigs.js";
2
​
3
Meteor.publish('admin-global-configs', function publishTasks() {
4
return Configs.find({})
5
})
Copied!
On the client side you need to use a special tracker from Meteor to follow when the collection is available and when it has been changed. To use it, you want to create three parts to your component:
  • One part to hold the other parts,
  • one part to connect to the collection and send the information as props to
  • the last part where the information will be displayed
For example we create a waiting consent component where we want participants to wait until a certain time before they can join the experiment.
Create the WaitingConsent.jsx component (don't forget it to set it in the client/main.js).
Import the important elements:
1
import { withTracker } from "meteor/react-meteor-data"
2
import { Configs } from '../../../shared/api/collectionAdminGlobalConfigs'
Copied!
Then create the first part in which you call the second part.
1
export default class WaitingConsent extends Component {
2
render() {
3
​
4
return (
5
<div>
6
{/* Load the db data which loads the page contents */}
7
<WaitingConsentPageContents {...this.props} />
8
</div>
9
)
10
}
11
}
Copied!
Create the third and second part (in this order):
1
class WaitingConsentPage extends Component {
2
​
3
render() {
4
const { loading, now, timeToStart } = this.props
5
​
6
if (loading) {
7
return (
8
<div>Loading...</div>
9
)
10
}
11
​
12
let difference = +timeToStart - +now
13
​
14
// If waiting for the time, show the countdown and instructions
15
if (difference > 1) {
16
return (
17
<div>
18
<h3>Countdown</h3>
19
...
20
</div>
21
</div>
22
​
23
)
24
}
25
​
26
// otherwise return consent page
27
return (
28
<div>
29
<h3>Consent</h3>
30
...
31
</div>
32
)
33
}
34
}
35
​
36
WaitingConsentPageContents = withTracker(rest => {
37
​
38
// Suscribe to collection information, and return nothing as long as it is loading
39
const loading = !Meteor.subscribe("admin-global-configs").ready()
40
if (loading) {
41
return {
42
loading
43
}
44
}
45
​
46
// Get the globalConfigs collection
47
const globalConfigs = Configs.find({}).fetch()[0] ?? {}
48
const timeToStart = new Date(globalConfigs.timeToStart)
49
​
50
// Get time now (makes sur the whole process is synced)
51
const now = new Date(TimeSync.serverTime(null, 1000))
52
​
53
return {
54
loading,
55
timeToStart,
56
now,
57
};
58
​
59
})(WaitingConsentPage)
Copied!
You can see how in the last component we are subscribing to the collection, waiting for it to load, and then sending this information to the other component.
Last modified 4mo ago