ioserver 1.5.3 โ 2.0.3
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 +244 -287
- package/dist/BaseClasses.d.ts +301 -0
- package/dist/BaseClasses.d.ts.map +1 -0
- package/dist/BaseClasses.js +281 -0
- package/dist/BaseClasses.js.map +1 -0
- package/dist/IOServer.d.ts +260 -0
- package/dist/IOServer.d.ts.map +1 -0
- package/dist/IOServer.js +656 -0
- package/dist/IOServer.js.map +1 -0
- package/dist/IOServerError.d.ts +80 -0
- package/dist/IOServerError.d.ts.map +1 -0
- package/dist/IOServerError.js +85 -0
- package/dist/IOServerError.js.map +1 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +50 -0
- package/dist/index.js.map +1 -0
- package/package.json +67 -45
- package/build/ioserver.js +0 -580
- package/build/ioserver.js.map +0 -13
package/README.md
CHANGED
@@ -1,309 +1,266 @@
|
|
1
|
-
# IOServer
|
1
|
+
# ๐ IOServer
|
2
2
|
|
3
|
-
[](https://badge.fury.io/js/ioserver)
|
4
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
5
|
+
[](https://github.com/x42en/IOServer/actions)
|
6
|
+
[](https://codecov.io/gh/x42en/IOServer)
|
7
|
+
[](https://www.typescriptlang.org/)
|
4
8
|
|
5
|
-
|
6
|
-
[](https://www.npmjs.org/package/ioserver)
|
7
|
-
[](https://travis-ci.org/x42en/IOServer)
|
8
|
-
[](https://snyk.io/test/github/x42en/ioserver)
|
9
|
+
**A powerful, production-ready framework for building real-time applications with HTTP and WebSocket support.**
|
9
10
|
|
11
|
+
IOServer combines the speed of Fastify with the real-time capabilities of Socket.IO, providing a unified architecture for modern web applications. Built with TypeScript and designed for scalability, it offers a clean separation of concerns through services, controllers, managers, and watchers.
|
10
12
|
|
11
|
-
|
13
|
+
## โจ Features
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
- ๐ **High Performance** - Built on Fastify for maximum HTTP throughput
|
16
|
+
- โก **Real-time Communication** - Integrated Socket.IO for WebSocket connections
|
17
|
+
- ๐๏ธ **Modular Architecture** - Clean separation with Services, Controllers, Managers, and Watchers
|
18
|
+
- ๐ **Security First** - Built-in CORS, error handling, and validation
|
19
|
+
- ๐ **TypeScript Native** - Full type safety and IntelliSense support
|
20
|
+
- ๐งช **Fully Tested** - Comprehensive test suite with 95%+ coverage
|
21
|
+
- ๐ง **Configuration Driven** - JSON-based routing and flexible configuration
|
22
|
+
- ๐ฆ **Production Ready** - Memory leak detection, performance monitoring, and error handling
|
23
|
+
|
24
|
+
## ๐ Quick Start
|
25
|
+
|
26
|
+
### Installation
|
19
27
|
|
20
|
-
## Install
|
21
|
-
|
22
|
-
Install with npm:
|
23
|
-
```bash
|
24
|
-
npm install ioserver
|
25
|
-
```
|
26
|
-
|
27
|
-
## Basic Usage
|
28
|
-
|
29
|
-
Require the module:
|
30
|
-
```coffeescript
|
31
|
-
IOServer = require 'ioserver'
|
32
|
-
|
33
|
-
app = new IOServer
|
34
|
-
verbose: true
|
35
|
-
```
|
36
|
-
|
37
|
-
Add manager using:
|
38
|
-
```coffeescript
|
39
|
-
app.addManager
|
40
|
-
name: 'manager_name'
|
41
|
-
manager: ManagerClass
|
42
|
-
```
|
43
|
-
|
44
|
-
Add services using:
|
45
|
-
```coffeescript
|
46
|
-
app.addService
|
47
|
-
name: 'service_name'
|
48
|
-
service: ServiceClass
|
49
|
-
```
|
50
|
-
|
51
|
-
Add watchers using:
|
52
|
-
```coffeescript
|
53
|
-
app.addWatcher
|
54
|
-
name: 'watcher_name'
|
55
|
-
watcher: WatcherClass
|
56
|
-
```
|
57
|
-
|
58
|
-
Add controller using:
|
59
|
-
```coffeescript
|
60
|
-
app.addController
|
61
|
-
name: 'controller_name'
|
62
|
-
controller: ControllerClass
|
63
|
-
```
|
64
|
-
|
65
|
-
Start the server...
|
66
|
-
```coffeescript
|
67
|
-
app.start()
|
68
|
-
```
|
69
|
-
|
70
|
-
|
71
|
-
## Extended usage
|
72
|
-
|
73
|
-
You can add services with Middlewares:
|
74
|
-
```coffeescript
|
75
|
-
app.addService
|
76
|
-
name: 'service_name'
|
77
|
-
service: ServiceClass
|
78
|
-
middlewares: [
|
79
|
-
AccessMiddleware
|
80
|
-
]
|
81
|
-
```
|
82
|
-
Middlewares are invoked at the socket connection to namespaces, they are usually used for restricting access, validate connection method and parameters.
|
83
|
-
|
84
|
-
You can send event from external process
|
85
|
-
```coffeescript
|
86
|
-
app.sendTo
|
87
|
-
event: 'event name'
|
88
|
-
data: data
|
89
|
-
```
|
90
|
-
|
91
|
-
to specific namespace ...
|
92
|
-
```coffeescript
|
93
|
-
app.sendTo
|
94
|
-
namespace: '/namespace'
|
95
|
-
event: 'event name'
|
96
|
-
data: data
|
97
|
-
```
|
98
|
-
|
99
|
-
... or specific room
|
100
|
-
```coffeescript
|
101
|
-
app.sendTo
|
102
|
-
namespace: '/namespace'
|
103
|
-
room: 'room_name'
|
104
|
-
event: 'event name'
|
105
|
-
data: data
|
106
|
-
```
|
107
|
-
and even specific socket.id
|
108
|
-
```coffeescript
|
109
|
-
app.sendTo
|
110
|
-
namespace: '/namespace'
|
111
|
-
sid: socket.id
|
112
|
-
event: 'event name'
|
113
|
-
data: data
|
114
|
-
```
|
115
|
-
|
116
|
-
You can add controller with Middlewares and routes prefix:
|
117
|
-
```coffeescript
|
118
|
-
app.addController
|
119
|
-
name: 'controller_name'
|
120
|
-
prefix: '/my_prefix/'
|
121
|
-
controller: ControllerClass
|
122
|
-
middlewares: [
|
123
|
-
RESTMiddleware
|
124
|
-
]
|
125
|
-
```
|
126
|
-
|
127
|
-
You cann add watchers class that will be launched at start using watch() method
|
128
|
-
```coffeescript
|
129
|
-
app.addWatcher
|
130
|
-
name: 'watcher_name'
|
131
|
-
watcher: WatcherClass
|
132
|
-
```
|
133
|
-
|
134
|
-
In order to meet the fastify requirements, some pre-requised are needed to setup REST endpoints.
|
135
|
-
1. First your JS class will define your accessible controller's methods
|
136
|
-
```coffeescript
|
137
|
-
module.exports = class HelloController
|
138
|
-
constructor: (@app) ->
|
139
|
-
|
140
|
-
_isAuthentified: (req, reply, next) ->
|
141
|
-
if not req.headers['x-authentication']?
|
142
|
-
return reply.forbidden()
|
143
|
-
|
144
|
-
next()
|
145
|
-
|
146
|
-
world: (req, reply) ->
|
147
|
-
return { message: "Hello world" }
|
148
|
-
|
149
|
-
display: (req, reply) ->
|
150
|
-
return { message: "Hello #{req.params.message}" }
|
151
|
-
|
152
|
-
restricted: (req, reply) ->
|
153
|
-
return { message: "Welcome on Private Area" }
|
154
|
-
```
|
155
|
-
|
156
|
-
2. Then setup a routes description file (by default it will be looked-up into a `routes/${controller_name}.json` directory at root level of your project). *You can use different location by specifying `routes` options on IOServer instanciation (see unit-tests for examples).*
|
157
|
-
```json
|
158
|
-
[
|
159
|
-
{
|
160
|
-
"method": "GET",
|
161
|
-
"url": "/",
|
162
|
-
"handler": "world"
|
163
|
-
},
|
164
|
-
{
|
165
|
-
"method": "GET",
|
166
|
-
"url": "/:message",
|
167
|
-
"handler": "display"
|
168
|
-
},
|
169
|
-
{
|
170
|
-
"method": "GET",
|
171
|
-
"url": "/private/",
|
172
|
-
"handler": "restricted",
|
173
|
-
"preValidation": "_isAuthentified"
|
174
|
-
}
|
175
|
-
]
|
176
|
-
```
|
177
|
-
**All routes options from [fastify](https://www.fastify.io/docs/latest/Routes/#full-declaration) are supported**
|
178
|
-
|
179
|
-
|
180
|
-
Common options are:
|
181
|
-
```coffeescript
|
182
|
-
app = require 'ioserver'
|
183
|
-
port: 8443 # change listening port
|
184
|
-
host: '192.168.1.10' # change listening host
|
185
|
-
mode: ['websocket'] # Set socket.io client
|
186
|
-
# transport modes
|
187
|
-
# default is:
|
188
|
-
# ['websocket','polling']
|
189
|
-
# available methods are:
|
190
|
-
# ['websocket','htmlfile','polling','jsonp-polling']
|
191
|
-
verbose: 'DEBUG' # set verbosity level
|
192
|
-
cookies: false # Enable cookie usage for
|
193
|
-
# Socket.io v3
|
194
|
-
|
195
|
-
cors: { # Set up CORS as requested
|
196
|
-
origin: 'http://mydomain.com' # in Socket.io v3
|
197
|
-
methods: ['GET','POST']
|
198
|
-
}
|
199
|
-
```
|
200
|
-
|
201
|
-
## Example
|
202
|
-
|
203
|
-
1. Write a simple class (singleChat.coffee)
|
204
|
-
```coffeescript
|
205
|
-
module.exports = class SingleChat
|
206
|
-
|
207
|
-
constructor: (@app) ->
|
208
|
-
|
209
|
-
replay: (socket, text) ->
|
210
|
-
console.log "Someone say: #{text}."
|
211
|
-
socket.broadcast.emit 'message', text
|
212
|
-
|
213
|
-
# Synchronous event are supported
|
214
|
-
sync_replay: (socket, text, callback) ->
|
215
|
-
console.log "Someone say: #{text}."
|
216
|
-
callback text
|
217
|
-
|
218
|
-
# All methods starting with '_' are meant private
|
219
|
-
# and will not be published
|
220
|
-
_notAccessible: (socket) ->
|
221
|
-
console.error "You should not be here !!"
|
222
|
-
```
|
223
|
-
|
224
|
-
2. Start server-side ioserver process (server.coffee)
|
225
|
-
```coffeescript
|
226
|
-
IOServer = require 'ioserver'
|
227
|
-
ChatService = require './singleChat'
|
228
|
-
|
229
|
-
app = new IOServer()
|
230
|
-
|
231
|
-
app.addService
|
232
|
-
name: 'chat'
|
233
|
-
service: ChatService
|
234
|
-
|
235
|
-
app.start()
|
236
|
-
```
|
237
|
-
|
238
|
-
3. Compile and run server
|
239
|
-
```bash
|
240
|
-
coffee -c *.coffee
|
241
|
-
node server.js
|
242
|
-
```
|
243
|
-
|
244
|
-
4. Write simple client wich interact with server class method as socket.io events
|
245
|
-
```coffeescript
|
246
|
-
$ = require 'jquery'
|
247
|
-
io = require 'socket.io-client'
|
248
|
-
NODE_SERVER = 'Your-server-ip'
|
249
|
-
NODE_PORT = 'Your-server-port' # Default 8080
|
250
|
-
|
251
|
-
socket = io.connect "http://#{NODE_SERVER}:#{NODE_PORT}/chat"
|
252
|
-
|
253
|
-
# When server emit action
|
254
|
-
socket.on 'message', msg, ->
|
255
|
-
$('.message_list').append "<div class='message'>#{msg}</div>"
|
256
|
-
|
257
|
-
# Jquery client action
|
258
|
-
$('button.send').on 'click', ->
|
259
|
-
msg = $('input[name="message"]').val()
|
260
|
-
socket.emit 'replay', msg
|
261
|
-
|
262
|
-
# You can also use callback for synchronous actions
|
263
|
-
$('button.send').on 'click', ->
|
264
|
-
msg = $('input[name="message"]').val()
|
265
|
-
socket.emit 'sync_replay', msg, (data) ->
|
266
|
-
$('.message_list').append "<div class='message'>#{data}</div>"
|
267
|
-
|
268
|
-
```
|
269
|
-
For further case study you can also check de demo Chat application...
|
270
|
-
(link provided in few ~~days~~ weeks ;) )
|
271
|
-
|
272
|
-
## Developers
|
273
|
-
|
274
|
-
If you want to contribute to this project you are more than welcome !
|
275
|
-
|
276
|
-
### Run tests
|
277
28
|
```bash
|
278
|
-
npm
|
29
|
+
npm install ioserver
|
30
|
+
# or
|
31
|
+
yarn add ioserver
|
32
|
+
```
|
33
|
+
|
34
|
+
### Basic Usage
|
35
|
+
|
36
|
+
```typescript
|
37
|
+
import { IOServer, BaseService, BaseController } from 'ioserver';
|
38
|
+
|
39
|
+
// Create a service for real-time functionality
|
40
|
+
class ChatService extends BaseService {
|
41
|
+
async sendMessage(socket: any, data: any, callback?: Function) {
|
42
|
+
// Handle real-time messaging
|
43
|
+
socket.broadcast.emit('new_message', data);
|
44
|
+
if (callback) callback({ status: 'success' });
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
// Create a controller for HTTP endpoints
|
49
|
+
class ApiController extends BaseController {
|
50
|
+
async getStatus(request: any, reply: any) {
|
51
|
+
reply.send({ status: 'OK', timestamp: Date.now() });
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
// Initialize and configure server
|
56
|
+
const server = new IOServer({
|
57
|
+
host: 'localhost',
|
58
|
+
port: 3000,
|
59
|
+
cors: {
|
60
|
+
origin: ['http://localhost:3000'],
|
61
|
+
methods: ['GET', 'POST'],
|
62
|
+
},
|
63
|
+
});
|
64
|
+
|
65
|
+
// Register components
|
66
|
+
server.addService({ name: 'chat', service: ChatService });
|
67
|
+
server.addController({ name: 'api', controller: ApiController });
|
68
|
+
|
69
|
+
// Start server
|
70
|
+
await server.start();
|
71
|
+
console.log('๐ Server running at http://localhost:3000');
|
72
|
+
```
|
73
|
+
|
74
|
+
## ๐๏ธ Architecture
|
75
|
+
|
76
|
+
IOServer provides four core component types for building scalable applications:
|
77
|
+
|
78
|
+
### ๐ก **Services** - Real-time Logic
|
79
|
+
|
80
|
+
Handle WebSocket connections and real-time events.
|
81
|
+
|
82
|
+
```typescript
|
83
|
+
class NotificationService extends BaseService {
|
84
|
+
async notify(socket: any, data: any, callback?: Function) {
|
85
|
+
// Real-time notification logic
|
86
|
+
socket.emit('notification', { message: data.message });
|
87
|
+
if (callback) callback({ delivered: true });
|
88
|
+
}
|
89
|
+
}
|
90
|
+
```
|
91
|
+
|
92
|
+
### ๐ **Controllers** - HTTP Endpoints
|
93
|
+
|
94
|
+
Handle HTTP requests with automatic route mapping from JSON configuration.
|
95
|
+
|
96
|
+
```typescript
|
97
|
+
class UserController extends BaseController {
|
98
|
+
async getUser(request: any, reply: any) {
|
99
|
+
const userId = request.params.id;
|
100
|
+
reply.send({ id: userId, name: 'John Doe' });
|
101
|
+
}
|
102
|
+
}
|
103
|
+
```
|
104
|
+
|
105
|
+
### ๐ง **Managers** - Shared Logic
|
106
|
+
|
107
|
+
Provide shared functionality across services and controllers.
|
108
|
+
|
109
|
+
```typescript
|
110
|
+
class DatabaseManager extends BaseManager {
|
111
|
+
async query(sql: string, params: any[]) {
|
112
|
+
// Database operations
|
113
|
+
return await this.db.query(sql, params);
|
114
|
+
}
|
115
|
+
}
|
116
|
+
```
|
117
|
+
|
118
|
+
### ๐ **Watchers** - Background Tasks
|
119
|
+
|
120
|
+
Handle background processes, monitoring, and scheduled tasks.
|
121
|
+
|
122
|
+
```typescript
|
123
|
+
class HealthWatcher extends BaseWatcher {
|
124
|
+
async watch() {
|
125
|
+
setInterval(() => {
|
126
|
+
// Monitor system health
|
127
|
+
this.checkSystemHealth();
|
128
|
+
}, 30000);
|
129
|
+
}
|
130
|
+
}
|
279
131
|
```
|
280
132
|
|
281
|
-
|
133
|
+
## ๐ Configuration
|
134
|
+
|
135
|
+
### Server Options
|
136
|
+
|
137
|
+
```typescript
|
138
|
+
const server = new IOServer({
|
139
|
+
host: 'localhost', // Server host
|
140
|
+
port: 3000, // Server port
|
141
|
+
verbose: 'INFO', // Log level
|
142
|
+
routes: './routes', // Route definitions directory
|
143
|
+
cors: {
|
144
|
+
// CORS configuration
|
145
|
+
origin: ['http://localhost:3000'],
|
146
|
+
methods: ['GET', 'POST', 'PUT', 'DELETE'],
|
147
|
+
credentials: true,
|
148
|
+
},
|
149
|
+
mode: ['websocket', 'polling'], // Socket.IO transport modes
|
150
|
+
});
|
151
|
+
```
|
152
|
+
|
153
|
+
### Route Configuration
|
154
|
+
|
155
|
+
Define HTTP routes in JSON files (e.g., `routes/api.json`):
|
156
|
+
|
157
|
+
```json
|
158
|
+
[
|
159
|
+
{
|
160
|
+
"method": "GET",
|
161
|
+
"url": "/users/:id",
|
162
|
+
"handler": "getUser"
|
163
|
+
},
|
164
|
+
{
|
165
|
+
"method": "POST",
|
166
|
+
"url": "/users",
|
167
|
+
"handler": "createUser"
|
168
|
+
}
|
169
|
+
]
|
170
|
+
```
|
282
171
|
|
283
|
-
|
172
|
+
## ๐งช Testing
|
173
|
+
|
174
|
+
IOServer includes comprehensive testing utilities and examples:
|
284
175
|
|
285
|
-
Use coffeescript to compile your tests
|
286
176
|
```bash
|
287
|
-
|
177
|
+
# Run all tests
|
178
|
+
npm test
|
179
|
+
|
180
|
+
# Test categories
|
181
|
+
npm run test:unit # Unit tests
|
182
|
+
npm run test:integration # Integration tests
|
183
|
+
npm run test:e2e # End-to-end tests
|
184
|
+
npm run test:performance # Performance tests
|
185
|
+
|
186
|
+
# Coverage report
|
187
|
+
npm run test:coverage
|
288
188
|
```
|
289
189
|
|
290
|
-
|
190
|
+
## ๐ Examples
|
191
|
+
|
192
|
+
### Real-time Chat Application
|
193
|
+
|
194
|
+
A complete chat application example is included in the `examples/` directory, showcasing:
|
195
|
+
|
196
|
+
- User authentication and management
|
197
|
+
- Real-time messaging
|
198
|
+
- Room-based conversations
|
199
|
+
- Typing indicators
|
200
|
+
- Connection management
|
201
|
+
- API endpoints for statistics
|
202
|
+
|
291
203
|
```bash
|
292
|
-
|
204
|
+
cd examples/chat-app
|
205
|
+
npm install
|
206
|
+
npm start
|
293
207
|
```
|
294
208
|
|
295
|
-
|
209
|
+
Visit `http://localhost:8080` to see the chat application in action.
|
210
|
+
|
211
|
+
## ๐ง API Reference
|
296
212
|
|
297
|
-
|
213
|
+
### Core Classes
|
298
214
|
|
299
|
-
|
215
|
+
- **`IOServer`** - Main server class
|
216
|
+
- **`BaseService`** - Base class for WebSocket services
|
217
|
+
- **`BaseController`** - Base class for HTTP controllers
|
218
|
+
- **`BaseManager`** - Base class for shared logic managers
|
219
|
+
- **`BaseWatcher`** - Base class for background watchers
|
300
220
|
|
301
|
-
|
302
|
-
|
221
|
+
### Key Methods
|
222
|
+
|
223
|
+
```typescript
|
224
|
+
// Server management
|
225
|
+
server.addService(options: ServiceOptions)
|
226
|
+
server.addController(options: ControllerOptions)
|
227
|
+
server.addManager(options: ManagerOptions)
|
228
|
+
server.addWatcher(options: WatcherOptions)
|
229
|
+
server.start(): Promise<void>
|
230
|
+
server.stop(): Promise<void>
|
231
|
+
|
232
|
+
// Real-time messaging
|
233
|
+
server.sendTo(options: SendToOptions): boolean
|
303
234
|
```
|
304
235
|
|
305
|
-
##
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
236
|
+
## ๐ค Contributing
|
237
|
+
|
238
|
+
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
|
239
|
+
|
240
|
+
1. Fork the repository
|
241
|
+
2. Create a feature branch: `git checkout -b feature/amazing-feature`
|
242
|
+
3. Commit your changes: `git commit -m 'Add amazing feature'`
|
243
|
+
4. Push to the branch: `git push origin feature/amazing-feature`
|
244
|
+
5. Open a Pull Request
|
245
|
+
|
246
|
+
## ๐ License
|
247
|
+
|
248
|
+
This project is licensed under the Apache-2.0 License - see the [LICENSE](LICENSE) file for details.
|
249
|
+
|
250
|
+
## ๐ Acknowledgments
|
251
|
+
|
252
|
+
- Built with [Fastify](https://www.fastify.io/) for high-performance HTTP
|
253
|
+
- Powered by [Socket.IO](https://socket.io/) for real-time communication
|
254
|
+
- Inspired by modern microservice architectures
|
255
|
+
|
256
|
+
## ๐ Support
|
257
|
+
|
258
|
+
- ๐ [Documentation](https://github.com/x42en/IOServer/wiki)
|
259
|
+
- ๐ [Issue Tracker](https://github.com/x42en/IOServer/issues)
|
260
|
+
- ๐ฌ [Discussions](https://github.com/x42en/IOServer/discussions)
|
261
|
+
|
262
|
+
---
|
263
|
+
|
264
|
+
<div align="center">
|
265
|
+
<strong>Built with โค๏ธ for the Node.js community</strong>
|
266
|
+
</div>
|