Although this looks like a bit more code, it's essentially the same logic we built up before in our application.js file, but it's a little better organized. When we instantiate a Chat object, we initialize a few variables with a null value. As we begin connecting to and configuring our chat instance, we'll populate these variables with the appropriate values.

After the variables are defined, we call an initialize method, which has the logic for fetching our token. When the token is received, we create a client and then send the client to the setupClient method which attempts to find or create the "general" channel. Once the channel is available, it's passed to a setupChannel method that stores a reference to the channel and calls the joinChannel method.

Finally, the join channel method checks that we haven't already joined the channel, then it joins the channel and logs the message that we created previously.