Skip to content

createClient

Creates a Socket.IO-backed URQL client. The connection is not started automatically — use the SocketQLProvider or call connect() manually.

function createClient(options?: ClientOptions): SocketQLClient

The returned SocketQLClient extends URQL’s Client with additional methods.

OptionTypeDefaultDescription
pathstring'/ws/'Socket.IO server path
transportsstring[]['websocket']Allowed transports
graphqlNamespacestring'graphql'Socket.IO namespace to connect to
withCredentialsbooleanSend cookies with cross-origin requests
autoConnectbooleanfalseConnect automatically on creation
auth() => Record<string, any>Return auth data for the connection
onConnect() => voidCalled on successful connection
onConnectError(error: ConnectionError) => voidCalled when the connection is rejected
MethodSignatureDescription
connect() => voidInitiate the WebSocket connection
disconnect() => voidClose the WebSocket connection
reconnect() => voidDisconnect and immediately reconnect (picks up new cookies/auth)
socket(nsp: string, opts?) => SocketCreate an additional Socket.IO namespace socket via the underlying manager
invalidate(target: string | DocumentNode | Array) => voidManually invalidate cached operation results
onConnect(cb: () => void) => () => voidSubscribe to successful connections. Returns unsubscribe.
onConnectError(cb: (error: ConnectionError) => void) => () => voidSubscribe to connection errors. Returns unsubscribe.
onReconnect(cb: () => void) => () => voidSubscribe to successful reconnections. Returns unsubscribe.
onReconnectAttempt(cb: (attempt: number) => void) => () => voidSubscribe to reconnection attempts. Returns unsubscribe.
onReconnectError(cb: (error: Error) => void) => () => voidSubscribe to reconnection errors. Returns unsubscribe.
onReconnectFailed(cb: () => void) => () => voidCalled when reconnection fails permanently. Returns unsubscribe.
class ConnectionError extends Error {
data?: Record<string, any>
}

The data property contains any structured context attached by the server via ConnectionRejectedError’s second argument.

// Single query by operation name
client.invalidate('Users')
// Single query by document node
client.invalidate(UsersDocument)
// Multiple queries
client.invalidate(['Users', ProjectsDocument])