{"id":21235974,"url":"https://github.com/lddl/fiber-long-poll","last_synced_at":"2025-07-10T17:31:50.591Z","repository":{"id":57545178,"uuid":"297908792","full_name":"LdDl/fiber-long-poll","owner":"LdDl","description":"Long Polling library. It's just a port of golongpoll but for fasthttp-based Fiber web-framework.","archived":false,"fork":false,"pushed_at":"2020-12-10T09:58:11.000Z","size":49,"stargazers_count":18,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T15:51:10.212Z","etag":null,"topics":["fasthttp","fiber","hacktoberfest","longpoll","longpolling"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LdDl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-23T08:47:57.000Z","updated_at":"2024-08-05T12:19:11.000Z","dependencies_parsed_at":"2022-09-16T23:21:52.316Z","dependency_job_id":null,"html_url":"https://github.com/LdDl/fiber-long-poll","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/LdDl/fiber-long-poll","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LdDl%2Ffiber-long-poll","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LdDl%2Ffiber-long-poll/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LdDl%2Ffiber-long-poll/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LdDl%2Ffiber-long-poll/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LdDl","download_url":"https://codeload.github.com/LdDl/fiber-long-poll/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LdDl%2Ffiber-long-poll/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264619108,"owners_count":23638407,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fasthttp","fiber","hacktoberfest","longpoll","longpolling"],"created_at":"2024-11-21T00:05:28.030Z","updated_at":"2025-07-10T17:31:49.264Z","avatar_url":"https://github.com/LdDl.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![GoDoc](https://godoc.org/github.com/LdDl/fiber-long-poll?status.svg)](https://godoc.org/github.com/LdDl/fiber-long-poll)\n[![Go Report Card](https://goreportcard.com/badge/github.com/LdDl/fiber-long-poll)](https://goreportcard.com/report/github.com/LdDl/fiber-long-poll)\n\n# Long polling library for [Fiber](https://github.com/gofiber/fiber) web-framework\n\nGolang long polling library for [fasthttp](https://github.com/valyala/fasthttp)-based web framework called [Fiber](https://github.com/gofiber/fiber).\n\nMakes web pub-sub easy via an HTTP long-poll server.\n\n## Table of Contents\n\n- [About](#about)\n- [Usage](#usage)\n- [Issues](#issues)\n- [License](#license)\n\n## About\nThis library is just a port of existing library for long polling https://github.com/jcuga/golongpoll, but for Fiber ecosystem.\nYou can read about it here https://github.com/jcuga/golongpoll#table-of-contents.\n\n## Usage\nHere is example code: [click!](example)\n\nGolang server side:\n```go\npackage main\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\tlp \"github.com/LdDl/fiber-long-poll/v2\"\n\t\"github.com/gofiber/fiber/v2\"\n\t\"github.com/gofiber/fiber/v2/middleware/cors\"\n\t\"github.com/valyala/fasthttp\"\n)\n\nvar (\n\tuserID = \"my_pretty_uuid\"\n)\n\nfunc main() {\n\n\tconfig := fiber.Config{\n\t\tErrorHandler: func(ctx *fiber.Ctx, err error) error {\n\t\t\tlog.Println(err)\n\t\t\treturn ctx.Status(fasthttp.StatusInternalServerError).JSON(errors.New(\"panic error\"))\n\t\t},\n\t\tIdleTimeout: 10 * time.Second,\n\t}\n\tallCors := cors.New(cors.Config{\n\t\tAllowOrigins:     \"*\",\n\t\tAllowHeaders:     \"Origin, Authorization, Content-Type, Content-Length, Accept, Accept-Encoding, X-HttpRequest\",\n\t\tAllowMethods:     \"GET, POST, PUT, DELETE\",\n\t\tExposeHeaders:    \"Content-Length\",\n\t\tAllowCredentials: true,\n\t\tMaxAge:           5600,\n\t})\n\n\tserver := fiber.New(config)\n\tserver.Use(allCors)\n\n\tmanager, err := lp.StartLongpoll(lp.Options{\n\t\tLoggingEnabled:                 false,\n\t\tMaxLongpollTimeoutSeconds:      120,\n\t\tMaxEventBufferSize:             100,\n\t\tEventTimeToLiveSeconds:         60 * 2,\n\t\tDeleteEventAfterFirstRetrieval: false,\n\t})\n\tif err != nil {\n\t\tlog.Printf(\"Failed to create manager: %q\", err)\n\t\treturn\n\t}\n\tdefer manager.Shutdown()\n\n\tgo generatingMessages(manager)\n\tserver.Get(\"/unread_messages\", GetMessages(manager))\n\n\terr = server.Listen(\":8080\")\n\tif err != nil {\n\t\tfmt.Printf(\"Can't start server due the error: %s\\n\", err.Error())\n\t}\n}\n\n// GetMessages Long polling request\nfunc GetMessages(manager *lp.LongpollManager) func(ctx *fiber.Ctx) error {\n\treturn func(ctx *fiber.Ctx) error {\n\t\tctx.Context().PostArgs().Set(\"timeout\", \"10\")\n\t\tctx.Context().PostArgs().Set(\"category\", fmt.Sprintf(\"unread_messages_for_%s\", userID))\n\t\treturn manager.SubscriptionHandler(ctx)\n\t}\n}\n\n// generatingMessages Generate some messages\nfunc generatingMessages(manager *lp.LongpollManager) {\n\ti := 0\n\tfor {\n\t\tmanager.Publish(fmt.Sprintf(\"unread_messages_for_%s\", userID), fmt.Sprintf(\"Number: %d\", i))\n\t\ti++\n\t\ttime.Sleep(3 * time.Second)\n\t}\n}\n```\n\nJavaScript client side:\n```js\n\n\u003chtml\u003e\n    \u003chead\u003e\n        \u003ctitle\u003eFiber long polling example\u003c/title\u003e\n    \u003c/head\u003e\n    \u003cbody\u003e\n        \u003cul id=\"unred-messages\"\u003e\u003c/ul\u003e\n        \u003cscript src=\"http://code.jquery.com/jquery-1.11.3.min.js\"\u003e\u003c/script\u003e\n        \u003cscript\u003e\n            if(typeof window.console == 'undefined') { window.console = {log: function (msg) {} }; }\n            var sinceTime = (new Date(Date.now())).getTime();\n            (function poll() {\n                var timeout = 45;  // in seconds\n                var optionalSince = \"\";\n                if (sinceTime) {\n                    optionalSince = \"\u0026since_time=\" + sinceTime;\n                }\n                var pollUrl = `http://localhost:8080/unread_messages`;\n                // how long to wait before starting next longpoll request in each case:\n                var successDelay = 10;  // 10 ms\n                var errorDelay = 3000;  // 3 sec\n                $.ajax({ url: pollUrl,\n                    success: function(data) {\n                        if (data \u0026\u0026 data.events \u0026\u0026 data.events.length \u003e 0) {\n                            // got events, process them\n                            // NOTE: these events are in chronological order (oldest first)\n                            for (var i = 0; i \u003c data.events.length; i++) {\n                                // Display event\n                                var event = data.events[i];\n                                $(\"#unred-messages\").append(\"\u003cli\u003e\" + JSON.stringify(event.data) + \" at \" + (new Date(event.timestamp).toLocaleTimeString()) +  \"\u003c/li\u003e\")\n                                // Update sinceTime to only request events that occurred after this one.\n                                sinceTime = event.timestamp;\n                            }\n                            console.log(data.events);\n                            // success!  start next longpoll\n                            setTimeout(poll, successDelay);\n                            return;\n                        }\n                        if (data \u0026\u0026 data.timeout) {\n                            console.log(\"No events, checking again.\");\n                            // no events within timeout window, start another longpoll:\n                            setTimeout(poll, successDelay);\n                            return;\n                        }\n                        if (data \u0026\u0026 data.error) {\n                            console.log(\"Error response: \" + data.error);\n                            console.log(\"Trying again shortly...\")\n                            setTimeout(poll, errorDelay);\n                            return;\n                        }\n                        // We should have gotten one of the above 3 cases:\n                        // either nonempty event data, a timeout, or an error.\n                        console.log(\"Didn't get expected event data, try again shortly...\");\n                        setTimeout(poll, errorDelay);\n                    }, dataType: \"json\",\n                error: function (data) {\n                    console.log(\"Error in ajax request--trying again shortly...\");\n                    setTimeout(poll, errorDelay);  // 3s\n                }\n                });\n            })();\n        \u003c/script\u003e\n    \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## Support\nIf you have troubles or questions please [open an issue](https://github.com/LdDl/fiber-long-poll/issues/new).\n\n## License\nYou can check it [here](LICENSE.md)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flddl%2Ffiber-long-poll","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flddl%2Ffiber-long-poll","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flddl%2Ffiber-long-poll/lists"}