Skip to content

Runtime Adapters

The SDK core stays lean. It depends on fetch and accepts an injected browser-style WebSocket factory when the runtime does not provide one.

Browser

Modern browsers provide both globals:

ts
import { 
createSimulatorClient
} from '@my-swu/simulator-client'
const
simulatorClient
=
createSimulatorClient
({
// Browsers already provide fetch and WebSocket globals.
baseUrl
: 'https://simulator.example.com',
})

Node

Node 18+ provides fetch, but not WebSocket:

ts
import 
WebSocket
from 'ws'
import {
createSimulatorClient
} from '@my-swu/simulator-client'
import type {
WebSocketFactory
} from '@my-swu/simulator-client'
// `ws` gives Node the browser-style constructor the SDK expects. const
nodeWebSocketFactory
:
WebSocketFactory
=
url
=> new
WebSocket
(
url
)
const
simulatorClient
=
createSimulatorClient
({
baseUrl
: 'http://127.0.0.1:4000',
// Required when opening match sessions from Node.
webSocketFactory
:
nodeWebSocketFactory
,
})

If you only use HTTP methods, you do not need a socket factory.

Edge runtimes

Edge runtimes vary. If fetch exists but socket APIs differ, keep HTTP calls in the edge function and open match sockets from the browser client.

Nuxt

Use one singleton client and open sockets only on the client side:

ts
import { 
createSimulatorClient
} from '@my-swu/simulator-client'
import type { SimulatorClient } from '@my-swu/simulator-client' let
simulatorClient
: SimulatorClient | undefined
export function
useSimulatorClient
(): SimulatorClient {
if (
simulatorClient
== null) {
const
config
=
useRuntimeConfig
()
// Reuse one SDK instance so resource helpers share the same config.
simulatorClient
=
createSimulatorClient
({
baseUrl
:
config
.
public
.
simulatorBaseUrl
,
}) } return
simulatorClient
}

Call matchSession.connect() from onMounted() or a <ClientOnly> block.

Released under the MIT License.