#socket url and authentication

1 messages · Page 1 of 1 (latest)

stark vessel
#

Hello, i have problems with socket connection.
How should the url look like and where can i get token ? from source code i see it tries to match subscriber id, but i cannot see it anywhere.

im not using react or angular.

stark vessel
gilded kindle
stark vessel
#

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

gilded kindle
#

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

stark vessel
#

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..

gilded kindle
#

Hi, sorry in a crazy few days, have it on my todo list. will update asap 🙏

stark vessel
#

Hello @gilded kindle when could i expect an update? 🙂

stoic hollow
#

@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

stark vessel
#

I can try with v1, but what is the socket url? Ws://localhost:3002 isnt working and how to pass authentication?

stoic hollow
#

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}`,
  },
});
stark vessel
#

ill try it next year, im a bit swamped with relations hehe

stoic hollow
#

sure, let me know if anything needed 😉

distant moon
#

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?

stark vessel
#

Ill try to make it work today, hit me up in dms maybe we can find something together

stark vessel
#

@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...
stark vessel
#

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.

magic flaxBOT
#

@stark vessel, you just advanced to level 3!

topaz temple
#

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.

stark vessel
#
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

topaz temple
#

thank you!

topaz temple
#

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)

faint zodiac
#

Yes @topaz temple API secret is for backend only

topaz temple
#

so this call should use a session token only, not an api secret?

faint zodiac
#

<@&943775970421264404> Can you please help?

native linden
#

The api key needs to be keept secret