Options
All
  • Public
  • Public/Protected
  • All
Menu

Module websocket

WebSocket Module

This package is an underlying module of webview platform for serving WebSocket. You might want to use @sociably/webview unless you want to serve your own web service.

Install

npm install @sociably/core @sociably/http @sociably/websocket
# or with yarn
yarn add @sociably/core @sociably/http @sociably/websocket

Docs

Check the package reference.

Setup

Back-end

import Sociably from '@sociably/core';
import Http from '@sociably/http';
import WebSocket from '@sociably/websocket';

const DEV = process.env.NODE_ENV !== 'production';

const app = Sociably.createApp({
modules: [
Http.initModule({ /* ... */ }),
],
platforms: [
WebSocket.initModule({ entryPath: '/websocket' }),
],
service: [
{ // same origin policy
provide: WebSocket.UpgradeVerifier,
withValue: ({ headers }) => headers.origin === 'https://your.domain.com',
},
]
}).onEvent(async ({ bot, event }) => {
// send a event when a connection is open
if (event.type === 'connect') {
await bot.send({
category: 'greeting',
type: 'hello',
payload: 'world',
});
}
});

Front-end

import Client from '@sociably/websocket/client';

const client = new Client({ url: '/websocket' });

client.onEvent(async ({ event }) => {
if (event.type === 'hello') {
await client.send({
category: 'greeting',
type: 'hello',
payload: 'websocket',
});
}
});

Index

Component

Other

Re-exports HttpRequestInfo
AnyVerifyLoginFn: VerifyLoginFn<any, unknown, unknown>
ClientLoginFn<User, Credential>: () => Promise<{ credential: Credential; user: User }>

Type parameters

Type declaration

    • (): Promise<{ credential: Credential; user: User }>
    • Returns Promise<{ credential: Credential; user: User }>

ConnIdentifier: { id: string; serverId: string }

Type declaration

  • id: string
  • serverId: string
ConnectEventValue: EventValue<"connection", "connect", null>
ConnectionEventValue: ConnectEventValue | DisconnectEventValue
DisconnectEventValue: EventValue<"connection", "disconnect", { reason: string | undefined }>
EventInput: { category?: string; payload?: unknown; type: string }

Type declaration

  • Optional category?: string
  • Optional payload?: unknown
  • type: string
EventValue<Category, Type, Payload>: { category: Category; payload: Payload; type: Type }

Type parameters

  • Category: string = string

  • Type: string = string

  • Payload = any

Type declaration

  • category: Category
  • payload: Payload
  • type: Type
UpgradeRequestInfo: Omit<HttpRequestInfo, "body">
VerifyLoginFn<User, AuthContext, Credential>: (request: UpgradeRequestInfo, credential: Credential) => Promise<OkVerifyLoginResult<User, AuthContext> | FailVerifyLoginResult>

Type parameters

Type declaration

    • (request: UpgradeRequestInfo, credential: Credential): Promise<OkVerifyLoginResult<User, AuthContext> | FailVerifyLoginResult>
    • Parameters

      Returns Promise<OkVerifyLoginResult<User, AuthContext> | FailVerifyLoginResult>

VerifyUpgradeFn: (request: UpgradeRequestInfo) => boolean | Promise<boolean>

Type declaration

WebSocketComponent: NativeComponent<unknown, UnitSegment<EventInput>>
WebSocketConfigs<User, Auth>: { dispatchMiddlewares?: MaybeContainer<WebSocketDispatchMiddleware>[]; entryPath?: string; eventMiddlewares?: MaybeContainer<WebSocketEventMiddleware<User, Auth>>[]; heartbeatInterval?: number }

Type parameters

Type declaration

WebSocketDispatchChannel: WebSocketConnection | WebSocketUserChannel | WebSocketTopicChannel
WebSocketDispatchResponse: DispatchResponse<WebSocketJob, WebSocketResult>
WebSocketEvent<Value, User>: Value & { channel: WebSocketConnection; platform: "websokcet"; user: User }

Type parameters

WebSocketEventContext<User, AuthContext, Value>: { bot: WebSocketBot; event: WebSocketEvent<Value, User>; metadata: WebSocketMetadata<AuthContext>; platform: "websocket"; reply: any }

Type parameters

Type declaration

WebSocketEventMiddleware<User, Auth>: EventMiddleware<WebSocketEventContext<User, Auth>, null>

Type parameters

WebSocketJob: { target: DispatchTarget; values: EventInput[] }

Type declaration

WebSocketMetadata<AuthContext>: { auth: AuthContext; connection: WebSocketConnection; request: UpgradeRequestInfo; source: "websocket" }

Type parameters

  • AuthContext

Type declaration

  • auth: AuthContext
  • connection: WebSocketConnection
  • request: UpgradeRequestInfo
  • source: "websocket"
WebSocketPlatformUtilities<User, Auth>: PlatformUtilities<WebSocketEventContext<User, Auth>, null, WebSocketJob, WebSocketDispatchFrame, WebSocketResult>

Type parameters

WebSocketResult: { connections: ConnIdentifier[] }

Type declaration

Generated using TypeDoc