Skip to main content
Version: 2x

Class: WebSocketMessageServer

Class for creating a WebSocket server and sending messages between development server and the React Native applications.

Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/messageSocketServer.ts

Hierarchy

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new WebSocketMessageServer(fastify)

Create new instance of WebSocketMessageServer and attach it to the given Fastify instance. Any logging information, will be passed through standard fastify.log API.

Parameters

NameTypeDescription
fastifyFastifyDevServerFastify instance to attach the WebSocket server to.

Overrides

WebSocketServer.constructor

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:92

Properties

fastify

Protected fastify: FastifyDevServer

Fastify instance from which server will receive upgrade connections.

Inherited from

WebSocketServer.fastify

Defined in

packages/repack/src/server/ws/WebSocketServer.ts:16


paths

Readonly paths: string[]

Inherited from

WebSocketServer.paths

Defined in

packages/repack/src/server/ws/WebSocketServer.ts:18


server

Readonly server: Server

An instance of the underlying WebSocket server.

Inherited from

WebSocketServer.server

Defined in

packages/repack/src/server/ws/WebSocketServer.ts:13


PROTOCOL_VERSION

Static Readonly PROTOCOL_VERSION: 2

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:39

Methods

broadcast

broadcast(method, params?): void

Send method broadcast to all connected clients.

Parameters

NameTypeDescription
methodstringMethod name to broadcast.
params?Record<string, any>Method parameters.

Returns

void

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:348


forwardRequest

forwardRequest(clientId, message): void

Send given request message to it's designated client's socket based on message.target. The target client must be connected, otherwise it will throw an error.

Parameters

NameTypeDescription
clientIdstringId of the client that requested the forward.
messagePartial<ReactNativeMessage>Message to forward.

Returns

void

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:204


forwardResponse

forwardResponse(message): void

Send given response message to it's designated client's socket based on message.id.clientId. The target client must be connected, otherwise it will throw an error.

Parameters

NameTypeDescription
messagePartial<ReactNativeMessage>Message to forward.

Returns

void

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:235


getClientSocket

getClientSocket(clientId): WebSocketWithUpgradeReq

Get client's WebSocket connection for given clientId. Throws if no such client is connected.

Parameters

NameTypeDescription
clientIdstringId of the client.

Returns

WebSocketWithUpgradeReq

WebSocket connection.

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:140


handleError

handleError(clientId, message, error): void

Process error by sending an error message to the client whose message caused the error to occur.

Parameters

NameTypeDescription
clientIdstringId of the client whose message caused an error.
messagePartial<ReactNativeMessage>Original message which caused the error.
errorErrorConcrete instance of an error that occurred.

Returns

void

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:156


onConnection

onConnection(socket, request): void

Process new client's WebSocket connection.

Parameters

NameTypeDescription
socketWebSocketIncoming WebSocket connection.
requestIncomingMessageUpgrade request for the connection.

Returns

void

Overrides

WebSocketServer.onConnection

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:358


parseMessage

parseMessage(data, binary): undefined | Partial<ReactNativeMessage>

Parse stringified message into a ReactNativeMessage.

Parameters

NameTypeDescription
datastringStringified message.
binaryanyAdditional binary data if any.

Returns

undefined | Partial<ReactNativeMessage>

Parsed message or undefined if parsing failed.

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:103


processServerRequest

processServerRequest(clientId, message): void

Process request message targeted towards this WebSocketMessageServer and send back the results.

Parameters

NameTypeDescription
clientIdstringId of the client who send the message.
messagePartial<ReactNativeMessage>The message to process by the server.

Returns

void

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:258


sendBroadcast

sendBroadcast(broadcasterId, message): void

Broadcast given message to all connected clients.

Parameters

NameTypeDescription
broadcasterIdundefined | stringId of the client who is broadcasting.
messagePartial<ReactNativeMessage>Message to broadcast.

Returns

void

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:307


shouldUpgrade

shouldUpgrade(pathname): boolean

Parameters

NameType
pathnamestring

Returns

boolean

Inherited from

WebSocketServer.shouldUpgrade

Defined in

packages/repack/src/server/ws/WebSocketServer.ts:45


upgrade

upgrade(request, socket, head): void

Parameters

NameType
requestIncomingMessage
socketSocket
headBuffer

Returns

void

Inherited from

WebSocketServer.upgrade

Defined in

packages/repack/src/server/ws/WebSocketServer.ts:49


isBroadcast

Static isBroadcast(message): boolean

Check if message is a broadcast request.

Parameters

NameTypeDescription
messagePartial<ReactNativeMessage>Message to check.

Returns

boolean

True if message is a broadcast request and should be broadcasted with sendBroadcast.

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:48


isRequest

Static isRequest(message): boolean

Check if message is a method request.

Parameters

NameTypeDescription
messagePartial<ReactNativeMessage>Message to check.

Returns

boolean

True if message is a request.

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:62


isResponse

Static isResponse(message): boolean

Check if message is a response with results of performing some request.

Parameters

NameTypeDescription
messagePartial<ReactNativeMessage>Message to check.

Returns

boolean

True if message is a response.

Defined in

packages/repack/src/server/ws/WebSocketMessageServer.ts:74