The next step is to replace our dummy user data with real user data, so we need to do a little bit of programming. You send user information to TalkJS as JavaScript data by creating a User object.

Find the following snippet of code:

var other = new Talk.User({
    id: "654321",
    name: "Sebastian",
    email: "[email protected]",
    photoUrl: "https://demo.talkjs.com/img/sebastian.jpg",
    welcomeMessage: "Hey, how can I help?"
});

Now, replace all the data with fields from your database - typically someone's user account. The id field uniquely identifies this user to TalkJS, so choose a value that will never change (typically, your internal database id will do). name, email and so on are synchronized whenever the User is created. Finally, photoUrl and welcomeMessage are optional so don't worry if you don't have a real user yet.

It's probably easiest to hardcode a particular user for now. For example, if your backend is written in PHP, you might end up with something like this:

<?php $user = $database.getUser(654321); ?>
var other = new Talk.User(
    <?php echo json_encode(array(
        "id" => strval($user->id),
        "name" => $user->name,
        "email" => $user->email,
        "photoUrl" => $user->photoUrl,
        "welcomeMessage" => "Hey, let's have a chat!"
    )); ?>
);

Note that you probably can't just copy and paste the code above and expect it to work right away - it depends on how your backend is structured.

If you reload now, you should see the same chatbox, but you're chatting with an actual user in your system.

Conversations

A bit further down, there's these lines of code:

var conversation = talkSession.getOrCreateConversation(Talk.oneOnOneId(me, other));
conversation.setParticipant(me);
conversation.setParticipant(other);

Note that method name, getOrCreateConversation. If the two given users had been in touch before, TalkJS automatically continues that conversation. If not, a new conversation is started and the other user's welcomeMessage is shown.

The call to Talk.oneOnOneId(me, other) generates a unique Conversation ID based on the IDs of me and other. This is useful if you just want to create a conversation between two people. The other of the arguments does not matter.

Note that the conversation can only be mounted once the current user is part of the conversation, otherwise you must call setParticipant to add them.

Conversation IDs

You can also choose to manage your own conversation IDs. This way, you have full control over which conversations exist for who. A Conversation ID can be anything: a product, a service, a booking, an order, or even a hash of a product and a user id. It depends on your site. All TalkJS needs is a unique key that identifies that topic, so we can tell the different conversations apart.

If you want to be in charge of creating Conversation IDs, call getOrCreateConversation like this:

var conversation = talkSession.getOrCreateConversation("order_83562938");

Please note that conversation IDs must be globally unique (per App ID).

Read more about choosing the right conversation ID.

Subjects

var conversation = talkSession.getOrCreateConversation("order_83562938");
conversation.setAttributes({
    subject: "Hair Wax 5 Gallons"
});

The subject key is optional and should describe the conversation in a human-friendly way. TalkJS will display it above the conversation and optionally in the Inbox's conversation feed. You can include some limited wiki-style formatting here.

Next steps

You have a great working chat now, but your current user isn't set up right yet, showing as "Alice" to all other users. Let's get that fixed.