Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read events from another user´s address #5

Open
Ivan-Dosev opened this issue Jan 5, 2023 · 1 comment
Open

How to read events from another user´s address #5

Ivan-Dosev opened this issue Jan 5, 2023 · 1 comment

Comments

@Ivan-Dosev
Copy link

I have tried to use the example provided by you. I send a message Hello World1 from my account, then subscribe to myself and read it back. However when I send a Hello World2, Hello World3... messages I get back only the first one Hello World1. I assume I am either not subscribing correctly or I am doing something wrong. Can you please guide me how to send and receive all messages from a specific user, not only the first one? Below is an example I have used.

func read(){

    do{

        let keyPair = try KeyPair(privateKey: "df9aae2ac8233ffa210a086c54059d02ba3247dab1130dad968f28f036326a83")
        let subscription = Subscription(filters: [.init(authors: [keyPair.publicKey],eventKinds: [.textNote])])

        let message = ClientMessage.subscribe(subscription)
        let relayUrl = URL(string: "wss://jiggytom.ddns.net")!
        let webSocketTask = URLSession(configuration: .default).webSocketTask(with: relayUrl)

        webSocketTask.resume()


        webSocketTask.send(.string(try! message.string())) { error in
            if let error = error {
                fatalError("Error: \(error)")
            }
        }
        
      
            webSocketTask.receive { result in
                switch result {
                case .success(let message):
                    switch message {
                    case .string(let text):
                       
                        let message = try! RelayMessage(text: text)
                        self.text = text
                        print(message)
                    default:
                        fatalError("Error: Received unknown message type")
                    }
                case .failure(let error):
                    fatalError("Error: \(error)")
                }
            }

    }catch{
        
    }
}



func write(){
    
    do{
        
        let keyPair = try KeyPair(privateKey: "df9aae2ac8233ffa210a086c54059d02ba3247dab1130dad968f28f036326a83")
        
        let event = try Event(keyPair: keyPair, kind: EventKind.textNote, tags: [], content: "Hello World1")
        
        print(event.id)

        let message = ClientMessage.event(event)
        

        let relayUrl = URL(string: "wss://jiggytom.ddns.net")!
        let webSocketTask = URLSession(configuration: .default).webSocketTask(with: relayUrl)

        webSocketTask.resume()

        webSocketTask.send(.string(try! message.string())) { error in
            if let error = error {
                fatalError("Error: \(error)")
            }
            

        }
    }catch{
        
    }
}
@iarechiga
Copy link

I don't know if this is the right way to handle this but you need to listen again every time you receive a new message. Just add task.receive at the end of your listening function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants