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)
})
...
Configuration
Creation options
js
path: '/infirmier-socket-io/',
transports: ["websocket"],
reconnectionDelay: 1000,
reconnectionDelayMax: 10000,
extraHeaders: {
"bearer-token": "mytoken"
},
debug: true
Service timeout
Default : 3000ms
js
app.service(name, { timeout: 10000 })
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) => {
...
})