Skip to content

Documentation / @super-line/server / createSocketServer

Function: createSocketServer()

createSocketServer<C, A>(contract, opts): SocketServer<C, A>

Defined in: index.ts:291

Create a server bound to a contract. Attach it to an http.Server, then call SocketServer.implement with your handlers. authenticate resolves each connection's { role, ctx } at the upgrade.

Type Parameters

C

C extends Contract

A

A extends object

Parameters

contract

C

the shared contract.

opts

ServerOptions<C, A>

server options; authenticate is required.

Returns

SocketServer<C, A>

the SocketServer.

Throws

nothing directly; handler throws become a typed SocketError to the client.

Example

ts
const srv = createSocketServer(api, {
  server,
  authenticate: (req) => ({ role: 'user' as const, ctx: { id: '1' } }),
})
srv.implement({
  shared: { join: async ({ room }, _ctx, conn) => { srv.room(room).add(conn); return { ok: true } } },
  user:   { say:  async ({ text }, ctx)        => ({ id: '...' }) },
})

Released under the MIT License.