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

Name Type Description
fastify FastifyDevServer Fastify 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

Name Type Description
method string Method 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

Name Type Description
clientId string Id of the client that requested the forward.
message Partial<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

Name Type Description
message Partial<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

Name Type Description
clientId string Id 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

Name Type Description
clientId string Id of the client whose message caused an error.
message Partial<ReactNativeMessage> Original message which caused the error.
error Error Concrete 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

Name Type Description
socket WebSocket Incoming WebSocket connection.
request IncomingMessage Upgrade 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

Name Type Description
data string Stringified message.
binary any Additional 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

Name Type Description
clientId string Id of the client who send the message.
message Partial<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

Name Type Description
broadcasterId undefined | string Id of the client who is broadcasting.
message Partial<ReactNativeMessage> Message to broadcast.

Returns

void

Defined in

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


shouldUpgrade

shouldUpgrade(pathname): boolean

Parameters

Name Type
pathname string

Returns

boolean

Inherited from

WebSocketServer.shouldUpgrade

Defined in

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


upgrade

upgrade(request, socket, head): void

Parameters

Name Type
request IncomingMessage
socket Socket
head Buffer

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

Name Type Description
message Partial<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

Name Type Description
message Partial<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

Name Type Description
message Partial<ReactNativeMessage> Message to check.

Returns

boolean

True if message is a response.

Defined in

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