Skip to content

Commit ef0b75f

Browse files
committed
Socketio is set to default client & error handling improved & docs improved
1 parent 1c66b59 commit ef0b75f

File tree

14 files changed

+726
-260
lines changed

14 files changed

+726
-260
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ Communicate with your feathers js (https://feathersjs.com/) server from flutter.
66

77
## Documentation
88

9-
Documentation is now available from: [https://dahkenangnon.github.io/flutter_feathersjs.dart/](https://dahkenangnon.github.io/flutter_feathersjs.dart/)
9+
Documentation is now available from: [https://dahkenangnon.github.io/flutter_feathersjs.dart/](https://dahkenangnon.github.io/flutter_feathersjs.dart/)
10+
11+
1. shit+alt+select
12+
2. alt+clik to add new cursor (a time your want)
13+
3. Now replace

docs/src/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module.exports = {
5555
'',
5656
'installation',
5757
'authentication',
58+
'socketio-is-default-client',
5859
'using-rest',
5960
'using-socketio',
6061
'how-to-upload-file',

docs/src/guide/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Introduction
22

3-
## :bird: flutter_feathersjs :bird
3+
## :bird: flutter_feathersjs :bird:
44

55
Communicate with your feathers js [https://feathersjs.com/](https://feathersjs.com/) server from flutter.
66

docs/src/guide/authentication.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,10 @@ Then reAutenticate user, if JWT still validated without request credentials from
140140
// why => print(e.message);
141141
}
142142
```
143+
144+
145+
146+
147+
148+
149+
Now, let's send request and listen to feathers js event :notes: :zzz: :car:

docs/src/guide/listening-event.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Realtime with FlutterFeatherJs
22

3-
You can listen to feathers js realtime event with scketio client
3+
You can listen to feathers js realtime event with listen method
44

5-
[Listen to realtime event with scketio client](using-socketio.html#listen)
5+
[Listen to realtime event with scketio client](socketio-is-default-client.html#listen)
Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
# Default Client
2+
3+
**Because we love the realtime side of feathers js, by default socketio's methods are available on FlutterFeathersjs.{methodName}**
4+
5+
You get exactly what feathers server send (_Serialization and Deserialization are not supported._) except in the listen method.
6+
7+
This client don't support file upload
8+
9+
Recommended: Use rest client to upload file
10+
11+
## find
12+
13+
Retrieves a list of all matching resources from the service
14+
15+
All message
16+
17+
```dart
18+
try {
19+
var messageResponse = await flutterFeathersjs.find(serviceName: "message",
20+
query: {});
21+
22+
// print(messageResponse); => feathers's find data format
23+
} on FeatherJsError catch (e) {
24+
25+
// When error is FeatherJsErrorType
26+
// if(e.type == FeatherJsErrorType.IS_SERVER_ERROR)
27+
// Check the error type as above and handle it
28+
} catch (er) {
29+
// Catch unknown error
30+
31+
}
32+
```
33+
34+
message matching certain query
35+
36+
```dart
37+
38+
try {
39+
// Find message with query: here with {"_id": "5f7643f0462f4348970cd32e"}
40+
var messageResponse = await flutterFeathersjs.find(serviceName: "message", query: {"_id": "5f7643f0462f4348970cd32e"});
41+
42+
// print(messageResponse); => feathers's find data format
43+
} on FeatherJsError catch (e) {
44+
45+
// When error is FeatherJsErrorType
46+
// if(e.type == FeatherJsErrorType.IS_SERVER_ERROR)
47+
// Check the error type as above and handle it
48+
} catch (er) {
49+
// Catch unknown error
50+
51+
}
52+
```
53+
54+
## get
55+
56+
Retrieve a single resource from the service with an `_id`
57+
58+
```dart
59+
60+
61+
try {
62+
// Get a single new with it _id
63+
var messageResponse = await flutterFeathersjs.get(serviceName: "message", objectId: "5f7643f0462f4348970cd32e");
64+
65+
// print(messageResponse); => feathers's get data format
66+
} on FeatherJsError catch (e) {
67+
68+
// When error is FeatherJsErrorType
69+
// if(e.type == FeatherJsErrorType.IS_SERVER_ERROR)
70+
// Check the error type as above and handle it
71+
} catch (er) {
72+
// Catch unknown error
73+
74+
}
75+
```
76+
77+
## create
78+
79+
Create a new resource with data.
80+
81+
```dart
82+
83+
84+
try {
85+
86+
// Create a message on the server without file upload
87+
var messageResponse = await flutterFeathersjs.create(
88+
serviceName: "message",
89+
data: {"title": "Using FlutterFeathersjs is easy", "content": "Yes very easy" , "author" :"5f7h43f0462f4348970cd32e"});
90+
91+
// print(messageResponse); => feathers's get data format
92+
} on FeatherJsError catch (e) {
93+
94+
// When error is FeatherJsErrorType
95+
// if(e.type == FeatherJsErrorType.IS_SERVER_ERROR)
96+
// Check the error type as above and handle it
97+
} catch (er) {
98+
// Catch unknown error
99+
100+
}
101+
```
102+
103+
## update
104+
105+
Completely replace a single resource with the `_id = objectId`
106+
107+
```dart
108+
109+
try {
110+
111+
var messageResponse = await flutterFeathersjs.update(
112+
objectId: "5f7h43f0462f4t48970cd32e",
113+
serviceName: "message",
114+
data: {"title": "Using FlutterFeathersjs is easy", "content": "Yes very easy" , "author" :"5f7h43f0462f4348970cd32e"});
115+
116+
// print(messageResponse); => feathers's get data format
117+
} on FeatherJsError catch (e) {
118+
119+
// When error is FeatherJsErrorType
120+
// if(e.type == FeatherJsErrorType.IS_SERVER_ERROR)
121+
// Check the error type as above and handle it
122+
} catch (er) {
123+
// Catch unknown error
124+
125+
}
126+
```
127+
128+
## patch
129+
130+
Completely replace a single resource with the `_id = objectId`
131+
132+
```dart
133+
try {
134+
135+
var messageResponse = await flutterFeathersjs.patch(
136+
objectId: "5f7h43f0462f4t48970cd32e",
137+
serviceName: "message",
138+
data: {"title": "Using FlutterFeathersjs is easy", "content": "Yes very easy" , "author" :"5f7h43f0462f4348970cd32e"});
139+
// print(messageResponse); => feathers's get data format
140+
} on FeatherJsError catch (e) {
141+
142+
// When error is FeatherJsErrorType
143+
// if(e.type == FeatherJsErrorType.IS_SERVER_ERROR)
144+
// Check the error type as above and handle it
145+
} catch (er) {
146+
// Catch unknown error
147+
148+
}
149+
```
150+
151+
## remove
152+
153+
Remove a single resource with `_id = objectId`:
154+
155+
```dart
156+
try {
157+
var messageResponse = await flutterFeathersjs.remove(serviceName: "message", objectId: "5f7643f0462f4348970cd32e");
158+
159+
// print(messageResponse); => feathers's remove data format
160+
} on FeatherJsError catch (e) {
161+
162+
// When error is FeatherJsErrorType
163+
// if(e.type == FeatherJsErrorType.IS_SERVER_ERROR)
164+
// Check the error type as above and handle it
165+
} catch (er) {
166+
// Catch unknown error
167+
168+
}
169+
```
170+
171+
## listen :zzz:
172+
173+
For realtime event listening, keep in mind these informations:
174+
175+
0. `On updated | patched | created | removed` event are all supported;
176+
1. You need a [StreamSubscription](https://api.flutter.dev/flutter/dart-async/StreamSubscription-class.html)
177+
2. You need a state manangment plugin like [flutter_bloc](https://pub.dev/packages/flutter_bloc)
178+
179+
Now assume that we want to listen to `On created` on `message` service in realtime
180+
181+
Add an event in your `message_event.dart`:
182+
183+
```dart
184+
185+
class FeatherCreatedMessageEvent extends MessageEvent {
186+
final Message message;
187+
188+
FeatherCreatedMessageEvent({@required this.message});
189+
190+
@override
191+
List<Object> get props => [message];
192+
}
193+
194+
```
195+
196+
Add a state in your `message_state.dart`:
197+
198+
```dart
199+
class FeatherCreatedMessageState extends MessageState {
200+
final String message;
201+
202+
FeatherCreatedMessageState({@required this.message});
203+
204+
@override
205+
List<Object> get props => [message];
206+
}
207+
208+
```
209+
210+
Now listen the event and broadcast it to your widget in your `message_bloc.dart`:
211+
212+
```dart
213+
214+
import 'dart:async';
215+
import 'package:flutter_feathersjs/src/helper.dart';
216+
217+
// Other required import
218+
219+
part 'message_event.dart';
220+
part 'message_state.dart';
221+
222+
class MessageBloc extends Bloc<MessageEvent, MessageState> {
223+
224+
// FlutterFeathersjs
225+
final FlutterFeathersjs flutterFeathersjs;
226+
227+
// StreamSubscription
228+
StreamSubscription streamSubscription;
229+
230+
231+
// MessageBloc constructor
232+
MessageBloc(
233+
{this.flutterFeathersjs})
234+
: super(MessageInitial()) {
235+
236+
// Listen to realtime event
237+
streamSubscription = flutterFeathersjs
238+
.listen<Message>(
239+
serviceName: "message",
240+
fromJson: Message.fromMap)
241+
.listen(
242+
243+
// onData:
244+
(event) {
245+
246+
// event is FeathersJsEventData<Message>
247+
// What event is sent by feathers js ?
248+
if(event.type == FeathersJsEventType.created){
249+
// Trigger flutter_bloc event
250+
add(FeatherCreatedMessageEvent(message: event));
251+
252+
}else if(event.type == FeathersJsEventType.removed){
253+
//
254+
} else if (event.type == FeathersJsEventType.patched){
255+
// ...
256+
}
257+
258+
},
259+
260+
261+
// onError:
262+
(e) {
263+
// e is a FeatherJsError
264+
// You can check what error occured: e.type
265+
266+
// Add the event to flutter_bloc
267+
add(MessageError(message: e.error));
268+
},
269+
270+
);
271+
}
272+
273+
@override
274+
Stream<MessageState> mapEventToState(
275+
MessageEvent event,
276+
) async* {
277+
try {
278+
279+
// Listen to flutter_bloc event which correspond to our case for realtime
280+
if (event is FeatherCreatedMessageEvent) {
281+
282+
// Change the state to allow ui update
283+
yield FeatherCreatedMessageState(message: event.message);
284+
}
285+
} catch (e) {
286+
//yield MessageError(message: "Error");
287+
}
288+
}
289+
290+
//This method should be called when a [Bloc] is no longer needed.
291+
// Just add it, nothing to customize except the streamSubscription var name
292+
@override
293+
Future<void> close() {
294+
streamSubscription.cancel();
295+
return super.close();
296+
}
297+
}
298+
299+
```

docs/src/guide/using-rest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Rest methods
1+
# Rest Client
22

33
****Feathers Js rest client for rest api call****
44

docs/src/guide/using-socketio.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Socketio methods
1+
# Socketio client
22

33
\***\*Feathers Js scketio client for realtime communication\*\***
44

@@ -168,7 +168,7 @@ try {
168168
}
169169
```
170170

171-
## listen
171+
## listen :zzz:
172172

173173
For realtime event listening, keep in mind these informations:
174174

0 commit comments

Comments
 (0)