Part 4: Adding Chats

Installing the Empirica Chat

First, you should stop running your experiment (e.g., press cntrl+c in the command line) to install the package for the Empirica chat.
Install it by running this in the command line:
1
meteor npm install --save @empirica/chat
Copied!

Adding a chat to the social stage

To add a chat to a component, you have to import it at the top of the client/game/SocialExposure.jsx component:
1
import { Chat } from "@empirica/chat";
Copied!
Then, still in the client/game/SocialExposure.jsx component, we add a chat component by replacing the return of the main render with this code:
1
return (
2
<div className="social-exposure">
3
<h3 className="title">Social Information</h3>
4
<p className="title">
5
{
6
otherPlayers.length > 1
7
? <strong>There are {otherPlayers.length} other players:</strong>
8
: <strong>There is one other player:</strong>
9
}
10
</p>
11
{otherPlayers.map(p => this.renderSocialInteraction(p))}
12
<div>
13
<p className="chat-title"><strong>Chat</strong></p>
14
<Chat player={player} scope={round} />
15
</div>
16
</div>
17
);
Copied!
Note that we need to assign this player as a property of the chat. We also need to define a scope, which determines in which element of Empirica the chat is created (the game, every round, every stage, etc.). Here, we want one chat per round, so we set the scope to the round.
This also means we need to call out the round from the props of this component. Replace this line in the main render:
1
const { game, player } = this.props;
Copied!
With this one:
1
const { game, player, round } = this.props;
Copied!
Overall, your SocialExposure.jsx component shoud look like this:
1
import React from "react";
2
import { Chat } from "@empirica/chat";
3
4
export default class SocialExposure extends React.Component {
5
renderSocialInteraction(otherPlayer) {
6
// Get the value or return NA if no value was entered
7
const value = otherPlayer.round.get("value") ?? "NA";
8
return (
9
<div className="alter" key={otherPlayer._id}>
10
<img src={otherPlayer.get("avatar")} className="profile-avatar" />
11
Guess: {value}
12
</div>
13
);
14
}
15
16
render() {
17
const { game, player, round } = this.props;
18
19
const otherPlayers = game.players.filter(p =>
20
player.get("neighbors").includes(p.get("nodeId"))
21
);
22
23
if (otherPlayers.length === 0) {
24
return null;
25
}
26
27
return (
28
<div className="social-exposure">
29
<h3 className="title">Social Information</h3>
30
<p className="title">
31
{
32
otherPlayers.length > 1
33
? <strong>There are {otherPlayers.length} other players:</strong>
34
: <strong>There is one other player:</strong>
35
}
36
</p>
37
{otherPlayers.map(p => this.renderSocialInteraction(p))}
38
<div>
39
<p className="chat-title"><strong>Chat</strong></p>
40
<Chat player={player} scope={round} />
41
</div>
42
</div>
43
);
44
}
45
}
46
Copied!

Styling the chat

You can now see the chat in the social stage. However, it does not look very pretty. We could use a customClassName on the chat component to style all the elements of the chat. However, it is easier for us to just style a few of the chat's elements.
Add this bit of styling at the end of the client/main.less :
1
/* -----------------
2
- Styling the chat -
3
----------------- */
4
5
.chat-title {
6
text-align: center;
7
8
color: white;
9
background-color: #394b59;
10
11
border: 1px solid hsl(0, 0%, 75%);
12
13
margin: 1rem 0 0 0;
14
}
15
16
.empirica-chat-container {
17
width: 100%;
18
19
.chat {
20
form {
21
padding: 0;
22
}
23
}
24
}
25
26
.empirica-chat-container .chat form {
27
padding: 0;
28
}
29
30
.chat-button-send, .chat-input {
31
min-height: 50px;
32
}
33
34
.empirica-chat-container .chat form .chat-footer .chat-input {
35
border-radius: 0%;
36
}
37
38
.empirica-chat-container .chat form .chat-footer .chat-button-send {
39
border-radius: 0%;
40
}
41
42
.messages {
43
border: 1px solid hsl(0, 0%, 75%);
44
max-height: 300px;
45
}
46
47
.empirica-chat-container .chat .messages .message {
48
margin: 2rem 0.5rem;
49
}
Copied!

Using factors to create conditions with and without chats:

You might want to create condition where there are chats and a condition without chats. We can do that with new Treatments and Factors.
Go back to the Admin Panel and create a new chat factor as a boolean factor so it will be true or false. Now create a treatment with chat set to true and a treatment with chat set to false.
Now let's use this information render the chat only if the chat factor is set to true . Change the return of the main render of the SocialExposure.jsx component with this code:
1
return (
2
<div className="social-exposure">
3
<h3 className="title">Social Information</h3>
4
<p className="title">
5
{
6
otherPlayers.length > 1
7
? <strong>There are {otherPlayers.length} other players:</strong>
8
: <strong>There is one other player:</strong>
9
}
10
</p>
11
{otherPlayers.map(p => this.renderSocialInteraction(p))}
12
{game.treatment.chat &&
13
<div>
14
<p className="chat-title"><strong>Chat</strong></p>
15
<Chat player={player} scope={round} />
16
</div>
17
}
18
</div>
19
);
Copied!
Last modified 3mo ago