Skip to content
On this page

Client guide

Installation

bash
npm install @jcbuisson/express-x-client socket.io-client

Example

js
import io from 'socket.io-client'
import expressXClient from '@jcbuisson/express-x-client'

const socket = io({ debug: true })

const app = expressXClient(socket)

const user = await app.service('user').findUnique({
   where: {
      id: 22,
   },
})

app.service('user').on('create', (user) => {
   console.log('CREATE USER event', user)
})
...

Handling exceptions

When an exception occurs during a service method execution on the server, the error is sent back to the client.

js
try {
   const { userId } = await app.service('auth').login(email, password)
   ...
} catch(err) {
   if (err === 'wrong-credentials') {
      ...
   }
}

Publish / subscribe

Service events callbacks

When an service event is sent by the server, the client can set a callback. In the following example, the callback is executed when the client receives a 'create' event from the 'User' service. Its only argument is the data returned by the method called - here the created User.

js
app.service('user').on('create', (createdUser) => {
   ...
})

Application event callbacks

When an application event is sent by the server, the client can set a callback with app.on(). In the following example, the callback is executed whenever the application-wide event 'expireAt' is sent to the client

js
app.on('expireAt', (date) => {
   ...
})