Function: socketIOServiceEntrypoint()
socketIOServiceEntrypoint(
options?):EntrypointAdapter<ServiceContext<{ }>>
Create a service adapter of an entrypoint served by socket.io. It needs the optional peer dependency socket.io (and socket.io-client on the clients).
A request is an emit of the request event ('message' by default) whose data holds the action's pattern and input, with an acknowledgement callback receiving the response {success, error, payload} (see ServiceResponseCodec): payload is the result, or error the whole error object when the action failed (no matching action, validation…). An emit without an acknowledgement still runs the action.
Each socket belongs to a client, identified by the id it gives when it connects (auth.id, else the id header, else the id query parameter; at most 200 characters), else by its socket id; its requests carry it as ServiceContext.clientId. The events pushed by Entrypoint.notify are emitted to every socket of the client, those of Entrypoint.broadcast to every socket of the namespace, with the event's arguments as the emit's arguments.
When a socket disconnects, its requests in progress are aborted (their abort signal fires). When the entrypoint stops, the new requests are answered with an EntrypointStoppedException error; once the requests in progress end, the socket.io server is closed (the sockets disconnected, with the attached HTTP server left open).
Parameters
options?
SocketIOServiceEntrypointOptions
The adapter options; by default it listens on port 3000 of all the interfaces with a server of its own.
Returns
EntrypointAdapter<ServiceContext<{ }>>
The adapter, to give to the service option of buildEntrypoints.
Example
import {Application} from 'lakutata'
import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
import {ServiceAction} from 'lakutata/decorator/ctrl'
import {socketIOServiceEntrypoint} from 'lakutata/adapter/socket.io'
import {io} from 'socket.io-client'
class EchoController extends Controller {
@ServiceAction({cmd: 'echo'})
public async echo(input: {text: string}): Promise<string> {
return input.text
}
}
Application.run({
id: 'echo.service',
name: 'Echo',
components: {
entrypoint: buildEntrypoints({
controllers: [EchoController],
service: socketIOServiceEntrypoint({port: 3000, options: {cors: {origin: '*'}}})
})
},
bootstrap: ['entrypoint']
})
//A client, identified as client-1 (the events pushed by entrypoint.notify('client-1', 'progress', 50) reach it)
const socket = io('http://localhost:3000', {auth: {id: 'client-1'}})
socket.on('progress', (percent: number): void => console.log(`${percent}%`))
socket.emit('message', {cmd: 'echo', text: 'hi'}, (response: {success: boolean, error: unknown, payload: unknown}): void => {
//{success: true, error: null, payload: 'hi'}
console.log(response)
})