#socket url and authentication
1 messages · Page 1 of 1 (latest)
after an hour i finally found where the JWT token is created
https://github.com/novuhq/novu/blob/7235e410366e0897ed3f44cc8974f62f5ff56044/libs/testing/src/user.session.ts#L147
now how to make the socket connection 🤔
EDIT: doesnt work, so digging more
Hi @stark vessel the token for the socket service is created automaticaly and available either behind the scene with the iframe embed or by using the react component: by using the useSocket hook: https://docs.novu.co/notification-center/react-components#realtime-sockets
im using flutter, so i cant use any of your SDKs.
is there a way to expose it on my end ? i was looking at api documentation and the authentication is missing.
i managed to find the correct call
final response = await http.post(
Uri.parse('http://10.0.2.2:4500/widgets/session/initialize'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'ApiKey apikey',
},
body: jsonEncode(<String, dynamic>{
'applicationIdentifier': 'appId',
'subscriberId': 'id',
}),
);
but its taking like a minute and i get empty body
I can try to create a small guide for you, but not sure would be able to do it. Basically you need to reverse how the react component initialize a session.
I can write something tomorrow ish
exactly, it would be nice to "split" it for the future
That would be amazing if you could do that!
we decided loong ago for flutter (cross platform) and mashing in somehow js to have notifications is iffy to me.
if i get session and proper example of websockets url, then it should be fine right?
plan is:
socket to get unread notifications (just count)
then fetch notifications (separate page upon clicking)
then the whole functionality when on page and active notification -> read, onclick etc..
Hi, sorry in a crazy few days, have it on my todo list. will update asap 🙏
Hello @gilded kindle when could i expect an update? 🙂
@stark vessel I see that the URL that you are hitting is missing the /v1 prefix, could you try with that?
like: http://10.0.2.2:3000/v1/widgets/session/initialize
then using that token you can initialize the socket connection
I can try with v1, but what is the socket url? Ws://localhost:3002 isnt working and how to pass authentication?
the default API port is 3000 and WS 3002
to create to socket connection in web app we do use the socket.io-client library, and the URL passed looks like this: http://localhost:3002 so it starts with http
then it's switched to ws
const socket = io(socketUrl, {
reconnectionDelayMax: 10000,
transports: ['websocket'],
auth: {
token: `${token}`,
},
});
Thanks! ill try that, im using https://pub.dev/packages/web_socket_channel so hopefully i can find the correct way of setting params
ill try it next year, im a bit swamped with relations hehe
sure, let me know if anything needed 😉
Hi, we also use Flutter and want to try Novu. @stark vessel Do you plan to share your progress on integrating Note into Flutter in some public repo?
Ill try to make it work today, hit me up in dms maybe we can find something together
@stoic hollow Hello! can you show me network url you send for socket? i have option to pass in token in url query param and dont see in docker that the url was hit.. no log whatsoever, just that nest is initialized
WebSocketChannelException: WebSocketChannelException: HttpException: Connection closed before full header was received, uri = http://localhost:3002/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVC...
seems like socket.io isnt really socket server, just acts like it. thats why websocketchannel doesnt work, because its proper. Nice that i found the issue. BUT this is really weird, because of this i have to have two socket clients.
@stark vessel, you just advanced to level 3!
trying to find more info about novu + flutter/dart -- curious if there ended up being any guides or notes written up about using the feed API with flutter? i couldn't find a lot of information about how to use the API if not using one of the premade components like react, etc.
Another one, now its just not me 🙂 there is no flutter guide, api reference is your friend, socketio client needed, websocketchannel doesnt work. You need to scout sourcecode
final response = await http.post(
Uri.parse('http://localhost:3000/v1/widgets/session/initialize'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'ApiKey x',
},
body: jsonEncode(<String, dynamic>{
'applicationIdentifier': 'x',
'subscriberId': 'x',
}),
);
NovuSession session = NovuSession.fromJson(jsonDecode(response.body));
print('RESPONSE');
print(session.data?.token);
IO.Socket socket = IO.io(
'http://localhost:3002/?token=${session.data?.token}',
IO.OptionBuilder().setReconnectionDelayMax(10000).setTransports(['websocket']).build(),
);
socket.onConnect((_) {
print('connected');
});
socket.onConnectError((data) => print(data));
socket.on('unseen_count_changed', (data) => print('unseen_count_changed $data'));
socket.onDisconnect((_) => print('disconnect'));
Flutter example for socket
thank you!
really appreciate the example! still looking at source code and trying to make sure understand the workflow. curious about the use of the API key for authorization in the headers -- isn't the API key supposed to be secret and not available on the client?
(i know the app identifier can be exposed in the client, but i thought API key is secret and for backend only)
Yes @topaz temple API secret is for backend only
so this call should use a session token only, not an api secret?
<@&943775970421264404> Can you please help?
The api key needs to be keept secret