@eleven-am/pondsocket 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,139 @@
1
+
2
+ # PondSocket
3
+
4
+ PondSocket is a fast, minimalist and bidirectional socket framework for NodeJS. Pond allows you to think of each action during a sockets lifetime as a request instead of a huge callback that exists inside the connection event.
5
+ ## Documentation
6
+
7
+ This is a Node.js module available through the npm registry.
8
+
9
+ ```bash
10
+ npm install @eleven-am/pondsocket
11
+ ```
12
+
13
+ PondSocket usage depends on the environment in which it is being used.
14
+
15
+ #### On the server
16
+
17
+ When using PondSocket, an endpoint is created. The endpoint is the gateway by which sockets actually connect to the server.
18
+ Multiple endpoints can be created but every endpoint is independent of the other, ie sockets on one endpoint cannot communicate with sockets on another endpoint.
19
+
20
+ ```js
21
+ import { PondSocket } from "@eleven-am/pondsocket";
22
+
23
+ const pond = new PondSocket();
24
+
25
+ const endpoint = pond.createEndpoint('/api/socket', (req, res, _endpoint) => {
26
+ const token = req.query.token;
27
+ if (!token)
28
+ return res.reject('No token provided');
29
+ res.accept({
30
+ assign: {
31
+ token
32
+ }
33
+ });
34
+ })
35
+ ```
36
+
37
+ While sockets connect through the endpoint, communication between sockets cannot occur on the endpoint level. Sockets have to join a channel to communicate
38
+ between themselves.
39
+
40
+ ```js
41
+ const channel = endpoint.createChannel(/^channel(.*?)/, (req, res, channel) => {
42
+ const isAdmin = req.clientAssigns.admin;
43
+ if (!isAdmin)
44
+ return res.reject('You are not an admin');
45
+
46
+ res.accept({
47
+ assign: {
48
+ admin: true,
49
+ joinedDate: new Date()
50
+ },
51
+ presence: {
52
+ state: 'online'
53
+ },
54
+ channelData: {
55
+ locked: true,
56
+ numberOfUsers: channel.presence.length
57
+ }
58
+ });
59
+ });
60
+ ```
61
+
62
+ A user goes through the createChannel function to join a channel.
63
+ When a user joins a channel, some private information can be assigned to the user. This assign could be viewed as a cookie that is only available serverside.
64
+ The presence is the current state of the user. When you reassign a new presence information to a user, all other users connected to the same channel are informed of the change.
65
+ This could be used as *user is typing*, *user is away*, etc. The channelData is information that is stored on the channel and accessible from anywhere the channel is available.
66
+ It can be anything from a boolean to an instance of a class. This data cannot be accessed from another channel as it is private to the channel.
67
+
68
+ ```js
69
+ channel.on('hello', (req, res, channel) => {
70
+ const users = channel.presence;
71
+ res.assign({
72
+ assign: {
73
+ pingDate: new Date(),
74
+ users: users.length
75
+ }
76
+ });
77
+
78
+ // res.reject('curse words are not allowed on a child friendly channel')
79
+ // channel.closeFromChannel(req.client.clientId);
80
+ })
81
+ ```
82
+
83
+ When a message is sent on a channel by a user, an event is triggered. The *on* function can be used to listen for these events. If the function is specified, it is called when the message is received.
84
+ You can choose to decline the message being sent, or you can allow the message to be sent as usual. You can also do all the normal assigns to the channel, or user.
85
+ In case there is no *on* function, the message will be sent without any action being taken.
86
+
87
+ #### On the browser
88
+
89
+ ```js
90
+ import { PondClient } from "@eleven-am/pondsocket/client";
91
+
92
+ export const socket = new PondClient('/api/socket', {});
93
+ socket.connect();
94
+ ```
95
+
96
+ The browser compatible package can be imported from @eleven-am/pondsocket/client.
97
+ AN url string is provided to the class along with other url params, like token.
98
+
99
+ Multiple classes can be created, but it is advised to use a single class throughout the application.
100
+ You can just create multiple channels and maintain the single socket connection.
101
+
102
+ ```js
103
+ const channelTopic = 'channel:one';
104
+ const options = {
105
+ username: 'eleven-am'
106
+ }
107
+
108
+ export const channel = socket.createChannel(channelTopic, options);
109
+ channel.join();
110
+ ```
111
+
112
+ When connected to the channel you can subscribe to the events from the channel.
113
+
114
+ ```js
115
+ const subscriptionPresence = channel.onPresenceUpdate(presence => {
116
+ // handle the presence changes of the channel
117
+ });
118
+
119
+ const subscriptionMessage = channel.onMessage((event, data) => {
120
+ // handle the message being received
121
+ });
122
+
123
+ // When done with the channel remember to unsubscribe from these listeners
124
+ subscriptionPresence.unsubscribe();
125
+ subscriptionMessage.unsubscribe();
126
+ ```
127
+
128
+ There are many other features available on the channel object. Since the application is completely typed,
129
+ suggestions should be provided by your IDE.
130
+
131
+ ```js
132
+ channel.broadcast('hello', {
133
+ name: 'eleven-am',
134
+ message: 'I am the man, man'
135
+ })
136
+
137
+ // channel.broadcastFrom broadcasts a message to everyone but the client that emitted the message
138
+ // channel.sendMessage sends a message to clients specified in the function
139
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -1,4 +1,4 @@
1
- import {BaseClass, default_t, PondDocument, Subscription} from "../pondBase";
1
+ import {default_t, Subscription} from "../pondBase";
2
2
  import {NewUser, PondAssigns, PondChannelData, PondMessage, PondPresence, ServerMessage} from "./types";
3
3
  import {PondSenders, ServerActions} from "./enums";
4
4
  import {ChannelHandler} from "./channelMiddleWare";
@@ -10,12 +10,7 @@ export interface ChannelInfo {
10
10
  assigns: PondPresence[];
11
11
  }
12
12
 
13
- export interface PondUser {
14
- presence: PondDocument<PondPresence>;
15
- assigns: PondDocument<PondAssigns>;
16
- }
17
-
18
- export declare class Channel extends BaseClass {
13
+ export declare class Channel {
19
14
  readonly name: string;
20
15
 
21
16
  /**
@@ -13,6 +13,7 @@ export declare type ChannelEvent = {
13
13
  payload: default_t;
14
14
  event: string;
15
15
  };
16
+
16
17
  export declare type IncomingMiddlewareRequest = {
17
18
  channelName: string;
18
19
  event: string;
@@ -23,4 +24,5 @@ export declare type IncomingMiddlewareRequest = {
23
24
  clientPresence: PondPresence;
24
25
  };
25
26
  };
27
+
26
28
  export declare type ChannelHandler = (req: IncomingMiddlewareRequest, res: PondResponse, channel: Channel) => void | Promise<void>;
@@ -5,14 +5,14 @@ import {IncomingConnection} from "./types";
5
5
  import internal from "stream";
6
6
  import {WebSocket, WebSocketServer} from "ws";
7
7
  import {IncomingMessage} from "http";
8
- import {BaseClass, default_t, PondPath, Resolver, ResponsePicker} from "../pondBase";
8
+ import {default_t, PondPath, Resolver, ResponsePicker} from "../pondBase";
9
9
  import {PondChannel, PondChannelHandler} from "./pondChannel";
10
10
  import {ChannelInfo} from "./channel";
11
11
  import {PondResponse} from "./pondResponse";
12
12
 
13
13
  export declare type EndpointHandler = (req: IncomingConnection, res: PondResponse<ResponsePicker.POND>, endpoint: Endpoint) => void;
14
14
 
15
- export declare class Endpoint extends BaseClass {
15
+ export declare class Endpoint {
16
16
  constructor(server: WebSocketServer, handler: EndpointHandler);
17
17
 
18
18
  /**
@@ -1,11 +1,11 @@
1
- import {BaseClass, default_t, PondPath} from "../pondBase";
1
+ import {default_t, PondPath} from "../pondBase";
2
2
  import {Channel, ChannelInfo} from "./channel";
3
3
  import {IncomingChannelMessage, IncomingJoinMessage, PondMessage, PondResponseAssigns, SocketCache} from "./types";
4
4
  import {PondResponse} from "./pondResponse";
5
5
 
6
6
  export declare type PondChannelHandler = (req: IncomingJoinMessage, res: PondResponse, channel: Channel) => void;
7
7
 
8
- export declare class PondChannel extends BaseClass {
8
+ export declare class PondChannel {
9
9
 
10
10
  /**
11
11
  * @desc Gets a list of all the channels in the endpoint.
@@ -3,12 +3,12 @@
3
3
  /// <reference types="node" />
4
4
  import {IncomingMessage, Server as HTTPServer} from "http";
5
5
  import {WebSocketServer} from "ws";
6
- import {BaseClass, PondPath} from "../pondBase";
6
+ import {PondPath} from "../pondBase";
7
7
  import {Endpoint, EndpointHandler} from "./endpoint";
8
8
  import internal from "stream";
9
9
  import {NextFunction} from "./socketMiddleWare";
10
10
 
11
- export declare class PondSocket extends BaseClass {
11
+ export declare class PondSocket {
12
12
 
13
13
  constructor(server?: HTTPServer, socketServer?: WebSocketServer);
14
14
 
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="JavaScriptLibraryMappings">
4
- <includedPredefinedLibrary name="Node.js Core" />
5
- </component>
6
- </project>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/pondsocket.iml" filepath="$PROJECT_DIR$/.idea/pondsocket.iml" />
6
- </modules>
7
- </component>
8
- </project>
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/temp" />
6
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- </content>
9
- <orderEntry type="inheritedJdk" />
10
- <orderEntry type="sourceFolder" forTests="false" />
11
- </component>
12
- </module>