Connecting the Layer Client to Layer’s servers allows authentication to procede and data to flow. For the Web SDK, either initiates authentication, or determines that you are already authenticated.

How to do it

var client = new layer.Client({
    appId: "layer:///apps/staging/YOUR-APP-ID",
    isTrustedDevice: false

// Standard authentication flow

// Alternate authentication flow
client.connectWithSession("Your-User-ID", "session-token");

For the difference between connect() and connectWithSession(), see Authentication.


Should you end your layer session, you can reconnect using the existing client:

// End the current Layer Session

// At a later time...

// Alternate
client.connectWithSession("Your-User-ID", "session-token");

Multiple Clients Not Supported

There can be only a single Layer Client instantiated for a given Application ID. Any attempt to create a new one will cause the old one to be destroyed. This means that

  • If you try to create two Clients in two different parts of your application, only one of them will be functional.
  • If you try to create a new Client as part of reauthentication, the old client will cease to exist, and event handlers that were listening to it will no longer trigger

Sharing a single Client throughout your application and throughout the lifespan of your application is the prefered way of using this library.

Trusted devices

The Client constructor takes a property isTrustedDevice which defaults to false. Setting this to true will cause the Client to store your Layer Session Token in persistent memory. Calling the constructor with isTrustedDevice: true and userId property will cause the Client to attempt to restore that Session Token. Suppose that isTrustedDevice is true and userId is “TestUser”, the Client will perform the following tests:

  • If there is no session cached for the user named “TestUser”, then the challenge event will be emitted, allowing authentication to proceed.
  • If there is a session for the user named “TestUser” but it has expired, then the challenge event will be emitted, allowing authentication to proceed.
  • If there is a session for “TestUser” and it is valid, then the ready event will be emitted.

You can change the value of isTrustedDevice anytime before calling connect() or connectWithSession().

Best practice

We recommend putting the connection code in exactly one place in your app — this may be when your app loads, or where your Client is initialized.

Online state manager

Client provides an onlineManager property, which provides a component for monitoring if the client appears to be online or offline. While there are browser events for reporting when the browser is online/offline, browsers are inconsistent in how these are interpreted. For example Chrome will report you as online if you’re connected to a wifi router, even if that router is not connected to the internet.

The manager sends all of its events to the client, allowing you to simply monitor online state with:

client.on('online', function(evt) {
    if (evt.isOnline) {
    } else {

You can also directly query the client for the browser’s current state:

if (client.isOnline) {
    var query = client.createQuery({
        model: layer.Query.Conversation
Demos Authentication